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語句結束必須是一個分號。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM