Linux 中 sqlite3 基本操作

https://www.runoob.com/sqlite/sqlite-commands.html

一 。linux 下安装数据库和创建一个数据库

1. Linux 下安装sqlite3 需要两个命令 即可

   (1) sudo apt-get install sqlite 

   (2) sudo apt-get install libsqlite3-dev

2. 安装完后,创建一个数据库,终端下输入命令 【sqlite3 数据库名字 】数据库名字以 .db 结尾格式

  创建数据库student.db   【 sqlite3 student.db 

       

二 。数据库命令是以 【.】  开头的;数据库语句是以【;】结尾的

1. 数据库命令 

  (1) .schema 表名    显示表结构 如:【 .schema student 】

  (2)【 .tables 】  显示表

  (3)【 .quit 】或 【 .exit 】 退出数据库控制界面

  

 2. 数据库语句

  (1)创建一个数据表:student 【 create table student (id int primary key,name char,age int,sex char); 】  

  

   (2)向表中插入数据  insert into 表名 values (值1,值2,值3,值4); 如:【 insert into student values (0,'zhang0',20,'m'); 】 没有返回错误信息则插入成功

   

  

  (3)查找语句 select *from 表名;

      查找表中所有内容显示 【 select *from student; 】

      查找某个字段(列信息)【 select id from student; 】

      按照某个条件查找 【 select * from student where age>25 and sex='m' 】 { 条件 and  or 表示 与 ,或 }

   

  (4)修改某个内容 update 表名 set 列名=新值 where 条件;    如:把 zhang0 的名字改为 liwang 【update  student  set  name=‘liwang’   where  name=’zhang0‘;  】

  

  (4)删除一条信息 :delete from 表名 where 条件;  如:删除名字为hello的一行内容 【 delete from student where name='hello'; 】

  

  

  (5)添加一列 : alter table 表名 add 列名 数据类型; 【alter table student add address char;

  

  【update student set address='beijing';】 把地址字段全部更新为beijing

  

  (5)删除一个表  drop table 表名;  【 drop table soc; 

   

  (6)sqlite3不支持删除一列信息,可以 把原来的表里面的需要的列复制到一个新表,重新命名:create table 新表名 as select 列名1,列名2,列名3,列名4 from 旧表名; 

    【 create table stu as select id,name,sex from student; 】 选择student表中的id,name,sex字段作为新表的字段

   

  (7)重新命名表 :alter table 旧表名 rename to 新表名;  【 alter table stu rename to student_new; 】  

   

   (8)select count(*) from sqlite_master where type="table" and name = "表名";

    注意:表示字符串可以是""或者''SQL语句结束必须是一个分号。

  转自:https://www.cnblogs.com/electronic/p/11000443.html

原文地址:https://www.cnblogs.com/DerekDeng/p/13646018.html