一、插入數(shù)據(jù) INSERT INTO 表(列...) VALUES(值...)
根據(jù)前面幾篇的內(nèi)容,我們可以很輕送的創(chuàng)建一個(gè)數(shù)據(jù)表,并向其中插入一些數(shù)據(jù),不多說(shuō),看例子:
復(fù)制代碼 代碼如下:
myqiao@ubuntu:~/My Documents/db$ sqlite3 test.db
-- Loading resources from /home/myqiao/.sqliterc
SQLite version 3.7.4
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
sqlite>
sqlite> CREATE TABLE Teachers(
...> Id integer PRIMARY KEY,
...> Name text NOT NULL,
...> Age integer CHECK(Age>22),
...> Country text DEFAULT 'USA');
sqlite> .tables
Teachers
sqlite>
sqlite> INSERT INTO Teachers VALUES(1,'Alice',25,'CHN');
sqlite> INSERT INTO Teachers VALUES(2,'Bob',25,'BRA');
sqlite> INSERT INTO Teachers(Id,Name,Age,Country) VALUES(3,'Charls',33,'USA');
sqlite> INSERT INTO Teachers(Name,Age) VALUES('Jhon',43);
sqlite> SELECT * FROM Teachers;
Id Name Age Country
---- --------------- --------------- ---------------
1 Alice 25 CHN
2 Bob 25 BRA
3 Charls 33 USA
4 Jhon 43 USA
sqlite>
很簡(jiǎn)單,創(chuàng)建了一個(gè) Teachers 表并向其中添加了四條數(shù)據(jù),設(shè)定了一些約束,其中有自動(dòng)增加的主鍵、默認(rèn)值等等。
二、修改數(shù)據(jù) UPDATE 表 SET 列 = '新值' 【W(wǎng)HERE 條件語(yǔ)句】
UPDATE 語(yǔ)句用來(lái)更新表中的某個(gè)列,如果不設(shè)定條件,則所有記錄的這一列都被更新; 如果設(shè)定了條件,則符合條件的記錄的這一列被更新, WHERE 子句被用來(lái)設(shè)定條件,如下例:
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT * FROM Teachers;
Id Name Age Country
---- --------------- --------------- ---------------
1 Alice 25 CHN
2 Bob 25 BRA
3 Charls 33 USA
4 Jhon 43 USA
sqlite>
sqlite>
sqlite> UPDATE Teachers SET Country='China';
sqlite> SELECT * FROM Teachers;
Id Name Age Country
---- --------------- --------------- ---------------
1 Alice 25 China
2 Bob 25 China
3 Charls 33 China
4 Jhon 43 China
sqlite>
sqlite>
sqlite> UPDATE Teachers SET Country='America' WHERE Id=3;
sqlite> SELECT * FROM Teachers;
Id Name Age Country
---- --------------- --------------- ---------------
1 Alice 25 China
2 Bob 25 China
3 Charls 33 America
4 Jhon 43 China
sqlite>
sqlite>
sqlite> UPDATE Teachers SET Country='India' WHERE Age30;
sqlite> SELECT * FROM Teachers;
Id Name Age Country
---- --------------- --------------- ---------------
1 Alice 25 India
2 Bob 25 India
3 Charls 33 America
4 Jhon 43 China
sqlite>
三、刪除數(shù)據(jù) DELETE FROM 表 【W(wǎng)HERE 條件語(yǔ)句】
如果設(shè)定 WHERE 條件子句,則刪除符合條件的數(shù)據(jù)記錄;如果沒有設(shè)定條件語(yǔ)句,則刪除所有記錄
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT * FROM Teachers;
Id Name Age Country
---- --------------- --------------- ---------------
1 Alice 25 India
2 Bob 25 India
3 Charls 33 America
4 Jhon 43 China
sqlite>
sqlite>
sqlite> DELETE FROM Teachers WHERE Age>30;
sqlite> SELECT * FROM Teachers;
Id Name Age Country
---- --------------- --------------- ---------------
1 Alice 25 India
2 Bob 25 India
sqlite>
sqlite>
sqlite> DELETE FROM Teachers;
sqlite> SELECT * FROM Teachers;
sqlite>
四、查找數(shù)據(jù) SELECT 列... FROM 表
為了后面的練習(xí),需要一些樣本數(shù)據(jù)。 首先將下面的 SQL 語(yǔ)句保存到 data.sql 文件中
復(fù)制代碼 代碼如下:
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;
BEGIN TRANSACTION;
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");
COMMIT;
然后在在終端執(zhí)行命令 .read data.sql,將數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中
復(fù)制代碼 代碼如下:
sqlite>
sqlite> .tables
Friends
sqlite> .read data.sql
sqlite> .tables
Cars Orders Teachers
sqlite>
可以看到,Cars 表和 Orders 表已經(jīng)導(dǎo)入到數(shù)據(jù)庫(kù)中,現(xiàn)在可以查詢了
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT * FROM Cars;
Id Name Cost
---- --------------- ---------------
1 Audi 52642
2 Mercedes 57127
3 Skoda 9000
4 Volvo 29000
5 Bentley 350000
6 Citroen 21000
7 Hummer 41400
8 Volkswagen 21600
sqlite> SELECT * FROM Orders;
Id OrderPrice Customer
---- --------------- ---------------
1 1200 Williamson
2 200 Robertson
3 40 Robertson
4 1640 Smith
5 100 Robertson
6 50 Williamson
7 150 Smith
8 250 Smith
9 840 Brown
10 440 Black
11 20 Brown
sqlite>
五、 限制返回?cái)?shù)量 SELECT 列... FROM 表 LIMIT 數(shù)量 OFFSET 位置
有時(shí)候數(shù)據(jù)庫(kù)中的數(shù)據(jù)太多,全部返回可不行,可以限制返回的數(shù)量,還可以設(shè)定返回的起始位置,如下:
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT * FROM Cars LIMIT 4;
Id Name Cost
---- --------------- ---------------
1 Audi 52642
2 Mercedes 57127
3 Skoda 9000
4 Volvo 29000
sqlite>
sqlite> SELECT * FROM Cars LIMIT 4 OFFSET 2;
Id Name Cost
---- --------------- ---------------
3 Skoda 9000
4 Volvo 29000
5 Bentley 350000
6 Citroen 21000
sqlite>
六、 別名 SELECT 列 AS 別名,列 AS 別名 FROM
我們可以給返回?cái)?shù)據(jù)集中的某些列起一個(gè)比較直觀的名字,比如把 Cost 改為"Price Of Car"
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT Name , Cost AS 'Price Of Car' FROM Cars;
Name Price Of Car
---- ---------------
Audi 52642
Merc 57127
Skod 9000
Volv 29000
Bent 350000
Citr 21000
Humm 41400
Volk 21600
sqlite>
七、 條件查詢 SELECT 列 FROM 表 【W(wǎng)HERE 條件語(yǔ)句】
一般的條件語(yǔ)句都是大于、小于、等于之類的,這里有幾個(gè)特別的條件語(yǔ)句
LIKE
--------------------------------------------------------------------------------
LIKE 用通配符匹配字符串
下劃線 _ 匹配一個(gè)字符串
百分號(hào) % 匹配多個(gè)字符串
LIKE 匹配字符串時(shí)不區(qū)分大小寫
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT * FROM Cars WHERE Name Like '____';
Id Name Cost
---- --------------- ---------------
1 Audi 52642
sqlite>
sqlite> SELECT * FROM Cars WHERE Name Like '%en';
Id Name Cost
---- --------------- ---------------
6 Citroen 21000
8 Volkswagen 21600
sqlite>
sqlite> SELECT * FROM Cars WHERE Name Like '%EN';
Id Name Cost
---- --------------- ---------------
6 Citroen 21000
8 Volkswagen 21600
sqlite>
GLOB
--------------------------------------------------------------------------------
GLOB 用通配符匹配字符串
下劃線 ? 匹配一個(gè)字符串
百分號(hào) * 匹配多個(gè)字符串
LIKE 匹配字符串時(shí),區(qū)分大小寫
BETWEEN 值1 AND 值2
--------------------------------------------------------------------------------
返回兩個(gè)值之間的數(shù)據(jù)集合。下面的語(yǔ)句查詢價(jià)格在 20000 到 55000 之間的車,都是好車啊。
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT * FROM Cars WHERE Cost BETWEEN 20000 AND 55000;
Id Name Cost
---- --------------- ---------------
1 Audi 52642
4 Volvo 29000
6 Citroen 21000
7 Hummer 41400
8 Volkswagen 21600
sqlite>
IN (集合)
--------------------------------------------------------------------------------
對(duì)應(yīng)列的值必須在集合中。下面的語(yǔ)句查找奧迪和悍馬的價(jià)格。
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT * FROM Cars WHERE Name IN ('Audi','Hummer');
Id Name Cost
---- --------------- ---------------
1 Audi 52642
7 Hummer 41400
sqlite>
八、 排序 ORDER BY 列 ASC (DESC)
指定某個(gè)列進(jìn)行排序,ASC 為升序,DESC 為降序。下面的語(yǔ)句查詢汽車品牌和價(jià)格,并以價(jià)格排序
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT Name, Cost FROM Cars ORDER BY Cost DESC;
Name Cost
---- ---------------
Bent 350000
Merc 57127
Audi 52642
Humm 41400
Volv 29000
Volk 21600
Citr 21000
Skod 9000
sqlite>
九、 區(qū)分 DISTINCT 列
有一些字段的值可能會(huì)出現(xiàn)重復(fù),比如訂單表中,一個(gè)客戶可能會(huì)有好幾份訂單,因此客戶的名字會(huì)重復(fù)出現(xiàn)。
到底有哪些客戶下了訂單呢?下面的語(yǔ)句將客戶名字區(qū)分出來(lái)。
復(fù)制代碼 代碼如下:
sqlite>
sqlite> Select * FROM Orders;
Id OrderPrice Customer
---- --------------- ---------------
1 1200 Williamson
2 200 Robertson
3 40 Robertson
4 1640 Smith
5 100 Robertson
6 50 Williamson
7 150 Smith
8 250 Smith
9 840 Brown
10 440 Black
11 20 Brown
sqlite>
sqlite> SELECT DISTINCT Customer FROM ORDERS;
Customer
---------------
Black
Brown
Robertson
Smith
Williamson
sqlite>
十、 分組 GROUP BY 列
分組和前面的區(qū)分有一點(diǎn)類似。區(qū)分僅僅是為了去掉重復(fù)項(xiàng),而分組是為了對(duì)各類不同項(xiàng)進(jìn)行統(tǒng)計(jì)計(jì)算。
比如上面的例子,我們區(qū)分出 5 個(gè)客戶,這 5 個(gè)客戶一共下了 11 個(gè)訂單,說(shuō)明很多客戶都下了不止一個(gè)訂單。
下面的語(yǔ)句統(tǒng)計(jì)每個(gè)客戶在訂單上總共花費(fèi)了多少錢。
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT sum(OrderPrice) AS Total, Customer FROM Orders GROUP BY Customer;
Total Customer
--------------- ---------------
440 Black
860 Brown
340 Robertson
2040 Smith
1250 Williamson
sqlite>
這里 Sum 是 SQLite 內(nèi)置的統(tǒng)計(jì)函數(shù),在這個(gè)例子中用來(lái)求每個(gè)顧客的訂單價(jià)格的和。
統(tǒng)計(jì)結(jié)果也可以設(shè)定返回條件,但是不能用 WHERE 子句,而是用 HAVING 子句,如下例,返回訂單總額大于 1000 的顧客。
復(fù)制代碼 代碼如下:
sqlite>
sqlite> SELECT sum(OrderPrice) AS Total, Customer FROM Orders
...> GROUP BY Customer HAVING sum(OrderPrice)>1000;
Total Customer
--------------- ---------------
2040 Smith
1250 Williamson
sqlite>
十一、 邏輯運(yùn)算符
有的查詢涉及的條件語(yǔ)句很復(fù)雜,是有好幾個(gè)條件語(yǔ)句經(jīng)過(guò)邏輯運(yùn)算得來(lái)的,一共有三種邏輯運(yùn)算符:
AND
OR
NOT
一般稍微了解點(diǎn)編程知識(shí)的應(yīng)該都沒問(wèn)題。
您可能感興趣的文章:- SQLite3 API 編程手冊(cè)
- SQLite 錯(cuò)誤碼整理
- SQLite學(xué)習(xí)手冊(cè)(SQLite在線備份)
- SQLite3 命令行操作指南
- 為SQLite3提供一個(gè)ANSI到UTF8的互轉(zhuǎn)函數(shù)
- SQLite 內(nèi)存數(shù)據(jù)庫(kù)學(xué)習(xí)手冊(cè)
- SQLite 入門教程三 好多約束 Constraints
- SQLite 入門教程二 SQLite的創(chuàng)建、修改、刪除表
- SQLite 入門教程一 基本控制臺(tái)(終端)命令
- Linux sqlite3 基本命令
- Android使用SQLite數(shù)據(jù)庫(kù)的簡(jiǎn)單實(shí)例
- android中sqlite的按條件查找的小例子
- 基于sqlite特殊字符轉(zhuǎn)義的實(shí)現(xiàn)方法
- 解析SQLite中的常見問(wèn)題與總結(jié)詳解
- 淺談SQLite時(shí)間函數(shù)的使用說(shuō)明與總結(jié)分析
- 深入SQLite基本操作的總結(jié)詳解
- Android畫圖并保存圖片的具體實(shí)現(xiàn)代碼
- android創(chuàng)建數(shù)據(jù)庫(kù)(SQLite)保存圖片示例