1. 檢視sqllite的版本資訊:
2 建立資料庫
3 退出sqlite指令行的方式:
sqlite>.quit 或 sqlite>.exit指令
4 列出目前顯示格式的配置
5 顯示資料庫表結構
6 向表中插入資料
7 設定導出目标
12 設定顯示模式: .mode 模式
有好幾種顯示模式,預設的是 list 顯示模式,一般我們使用 column 顯示模式,還有其他幾種顯示模式可以 .help 看 mode 相關内容。看看下面的圖,和上面是不是顯示的不一樣了?
13 設定 null 值顯示成什麼樣子: .nullvalue 你想要的null值格式
預設情況下null值什麼也不顯示,你可以設定成你自己想要的樣子
14 配置檔案 .sqliterc
如果我們每次進入指令行都要重新設定顯示格式,很麻煩,其實 .show 指令列出的所有設定項都可以儲存到一個 .sqliterc 檔案中,這樣每次進入指令行就自動設定好了。.sqlterc 檔案在 linux 下儲存在使用者的 home 目錄下,在 windows 下可以儲存到任何目錄下,但是需要設定環境變量讓資料庫引擎能找到它,感興趣的可以看看幫助。
15 自定義分隔符
16 顯示标題欄 .header on
不顯示标題欄:.header off
三.資料庫和表的相關指令
1、建立一個新的資料庫:sqlite3 檔案名
建立一個test.db資料庫檔案,打開控制台視窗,指令如下:
2、打開一個已經存在的資料庫:sqlite3已經存在的檔案名
建立一個資料庫和打開一個已經存在的資料庫指令是一模一樣的,如果存在,則打開。
3 建立資料表
create table table_name(field type1,fieldtype1,….);
table_name是要建立資料表的名稱,field x上是資料庫表内字段名字,typex則是字段類型。
如:該語句建立一個記錄學生資訊的資料表
sql的指令格式:所有sql指令都是以分号(;)結尾,兩個減号(--)則表示注釋
4 添加資料記錄
insert into table_name(列 field1,field2,….)values(值val1,val2,….);
val x為需要存入字段的值。
例如,往老師資訊表添加資料:
很簡單,建立了一個teachers表并向添加了四條資料,設定了一些限制,其中有自動增加的主鍵、預設值等等。
5、修改資料
update 表 set 列 = ‘新值’ 【where 條件語句】
update語句用來更新表中的某個列,如果不設定條件,則所有記錄的這一列都被更新;如果設定了條件,則符合條件的這一列被更新,where子句被用來設定條件,如下例:
6 删除資料 delete from 表 【where條件語句】
如果設定where條件子句,則删除符合條件的資料記錄;如果沒有設定條件語句,則删除所有記錄。
7 導入資料: .read 資料檔案
打開記事本,并将下列sql語句複制到記事本中,儲存為test.sql,在指令行環境中輸入
.read test.sql
begin transaction;
create table cars(id integer primary key, name text, cost integer);
insert into cars values(1,'audi',52642);
insert into cars values(2,'mercedes',57127);
insert into cars values(3,'skoda',9000);
insert into cars values(4,'volvo',29000);
insert into cars values(5,'bentley',350000);
insert into cars values(6,'citroen',21000);
insert into cars values(7,'hummer',41400);
insert into cars values(8,'volkswagen',21600);
commit;
create table orders(id integer primary key,
orderprice integer check(orderprice>0),customer text);
insert into orders(orderprice, customer) values(1200, "williamson");
insert into orders(orderprice, customer) values(200, "robertson");
insert into orders(orderprice, customer) values(40, "robertson");
insert into orders(orderprice, customer) values(1640, "smith");
insert into orders(orderprice, customer) values(100, "robertson");
insert into orders(orderprice, customer) values(50, "williamson");
insert into orders(orderprice, customer) values(150, "smith");
insert into orders(orderprice, customer) values(250, "smith");
insert into orders(orderprice, customer) values(840, "brown");
insert into orders(orderprice, customer) values(440, "black");
insert into orders(orderprice, customer) values(20, "brown");
8、查詢資料記錄
a查詢輸出列出資料記錄
select * from table_name;
b 限制輸出資料記錄數量
若資料庫中的資料太多,全部傳回可不行,可以限制傳回的數量,還可以設定傳回的起始位置
select * from table_name limit val;
c 升序輸出資料記錄
select * from table_name order by fieldasc;
d 降序輸出資料記錄
select * from table order by field desc;
e 條件查詢
select * from table_name where expression;
in(集合)
select * from table_name where field in(‘val1’,’val2’,’val3’);
between值1 and值2
select * from table_name where fieldbetween val1 and val2;
select * from cars where cost between 41400and 350000;
f、查詢記錄數目
select count(*) from table_name;
g、區分列資料
select distinct field from table_name;
有一些字段的值可能會重複出現,distinct去掉重複項,将列中各字段值單個列出。
h 别名select 列as别名,列as别名from
可以傳回資料集中的某些列起一個比較直覺的名字,比如把const改為”price of car”
l、條件查詢select列from表 【where條件語句】
一般的條件語句都shiite大于、小于、等于之類的,這裡有幾個特别的條件語句
like
-------------------------------
like 用通配符比對字元串
下劃線_ 比對一個字元串
百分号% 比對多個字元串
like比對字元串時不區分大小寫
glob
j 區分 distinct 列
有一些字段的值可能會出現重複,比如訂單表中,一個客戶可能會有好幾份訂單,是以客戶的名字會重複出現。
到底有哪些客戶下了訂單呢?下面的語句将客戶名字區分出來。
k 分組group by列
分組和前面的區分有一點類似。區分僅僅是為了去掉重複項,而分組是為了對各類不同項進行統計計算。
比如上面的例子,我們區分出5個客戶,這5個客戶一共11個訂單,說明很多客戶都下了不止一個訂單。
下面的語句的統計每個客戶在訂單上總共花費了多少錢。
9 建立索引
當說資料表存在大量記錄,索引有助于加快查找資料表速度。
create index index_name on table_name(field);
例,針對學生表stu_no字段,建立一個索引:
create index student_index on student_table(stu_no);
建立完成後,sqlite3在對該字段查詢時,會自動使用該索引。
10 删除資料表或索引
drop table table_name;
drop index index_name;
11 sqlite3存儲資料的類型
null:辨別一個null值
integer:整數類型
real:浮點數
text:字元串
blob:二進制數
12 sqlite3存儲資料的限制條件
sqlite常用限制條件如下:
primary key -主鍵
1) 主鍵的值必須唯一,用于辨別每一條記錄,如學生的學号
2) 主鍵同時也是一個索引,通過主鍵查找記錄速度較快
3) 主鍵如果是整數類型,該列的值可以自動增長
not null – 非空
限制列記錄不能為空,否則報錯
unique – 唯一:
除主鍵外,限制其它列的資料的值唯一
check – 條件檢查:
限制該列的值必須符合條件才可存入
default – 預設值:
列資料中的值基本都是一樣的,這樣的字段列可設為預設值
orderprice integer check(orderprice>0),
customer text);
create table friends(id integer primary key, name text unique not null,
sex text check(sex in ('m', 'f')));
create table if not exists reservations(id integer primary key,
customerid integer, day text);
insert into reservations(customerid, day) values(1, '2009-22-11');
create table books(id integer primary key, title text, author text,
isbn text default 'not available');
13 怎麼在已經建立的表中插入一列
alter table table-name
add column column-name column-type;