一、安裝SQLite3方法
1、字符界面
sudo apt-get install sqlite3
2、圖形界面
sudo apt-get install sqliteman
二、SQLite數(shù)據(jù)類型
SQLite具有以下五種基本數(shù)據(jù)類型:
1、integer:帶符號(hào)的整型(最多64位)。
2、real:8字節(jié)表示的浮點(diǎn)類型。
3、text:字符類型,支持多種編碼(如UTF-8、UTF-16),大小無(wú)限制。
4、blob:任意類型的數(shù)據(jù),大小無(wú)限制。
5、null:表示空值。
三、SQLite命令使用
1、創(chuàng)建、打開(kāi)數(shù)據(jù)庫(kù):
sqlite3 *.db
提示:當(dāng)*.db文件不存在時(shí),sqlite會(huì)創(chuàng)建并打開(kāi)數(shù)據(jù)庫(kù)文件。當(dāng)*.db文件存在時(shí),sqlite會(huì)打開(kāi)數(shù)據(jù)庫(kù)文件。
2、退出數(shù)據(jù)庫(kù)命令:
.quit 或 .exit
注意:SQL的語(yǔ)句格式:所有的SQL語(yǔ)句都是以分號(hào)結(jié)尾的,SQL語(yǔ)句不區(qū)分大小寫(xiě)。兩個(gè)減號(hào)“--”則代表注釋。
3、創(chuàng)建表:create 語(yǔ)句語(yǔ)法:
create table 表名稱 (列名稱1 數(shù)據(jù)類型, 列名稱2 數(shù)據(jù)類型, 列名稱3 數(shù)據(jù)類型, ...);創(chuàng)建一表格該表包含3列,列名分別是:“id”、“name”、“addr”。
在終端下輸入:create table persons (id integer, name text, addr text);
4、創(chuàng)建表:create 語(yǔ)句 (設(shè)置主鍵)
在用sqlite設(shè)計(jì)表時(shí),每個(gè)表都可以通過(guò)primary key手動(dòng)設(shè)置主鍵(整型),設(shè)置為主鍵的列數(shù)據(jù)不可以重復(fù)。語(yǔ)法:
create table 表名稱 (列名稱1 數(shù)據(jù)類型 primary key, 列名稱2 數(shù)據(jù)類型, 列名稱3 數(shù)據(jù)類型, ...);
在終端下輸入:create table persons (id integer primary key, name text, addr text);
5、查看表: .tables 查看數(shù)據(jù)表的結(jié)構(gòu): .schema
6、修改表:alter語(yǔ)句
在已有的表中添加或刪除列。語(yǔ)法:(添加、刪除-sqlite3暫不支持)
alter table 表名 add 列名 數(shù)據(jù)類型;
在終端下輸入:alter table persons add sex text ;
7、刪除表:drop table語(yǔ)句
用于刪除表(表的結(jié)構(gòu)、屬性以及表的索引也會(huì)被刪除)語(yǔ)法:drop table 表名稱;
在終端輸入:drop table persons ;
8、插入新行:inser into 語(yǔ)句(全部賦值)給一行中的所有列賦值。語(yǔ)法:insert into 表名 values (列值1, 列值2,列值3,列值4, ...);
注意:當(dāng)列值為字符串時(shí)要加上‘’號(hào)。
在終端下輸入:create table persons (id integer, name text, addr text);
insert into persons values (1, 'lucy', 'beijing');
9、插入新行:inser into 語(yǔ)句(部分賦值) 給一行中的部分列賦值。語(yǔ)法:insert into 表名 (列名1, 列名2, ...) values(列值1, 列值2, ...);
在終端下輸入:insert into persons (id, name) values (1, 'peter');
10、修改表中的數(shù)據(jù):update 語(yǔ)句
使用where根據(jù)匹配條件,查找一行或多行,根據(jù)查找的結(jié)果修改表中相應(yīng)行的列值(修改哪一列由列名指定)。
語(yǔ)法:update 表名 set 列1 = 值1 [, 列2 = 值2, ...] [匹配條件];
注意:當(dāng)表中有多列、多行符合匹配條件時(shí)會(huì)修改相應(yīng)的多行。當(dāng)匹配條件為空時(shí)則匹配所有。
11、匹配:where 子句
where 子句用于規(guī)定匹配的條件:等于(=)、不等于(<>)、大于(>)、小于(<)、大于等于(>=)、小于等于(<=)。
匹配條件語(yǔ)法:(基礎(chǔ)) where 列名 操作符 列值
在終端輸入:update persons set id=2, addr='tianjing' where name='peter';
12、刪除表中的數(shù)據(jù):delete 語(yǔ)句
使用where根據(jù)匹配條件,查找一行或多行,根據(jù)查找的結(jié)果刪除表中的查找到的行。語(yǔ)法:delete from 表名 [匹配條件];
注意:當(dāng)表中有多列、多行符合匹配條件時(shí)會(huì)刪除相應(yīng)的多行。
在終端輸入:delete from persons where name='peter';
13、查詢:select 語(yǔ)句(基礎(chǔ))
于從表中選取數(shù)據(jù),結(jié)果被存儲(chǔ)在一個(gè)結(jié)果表中(稱為結(jié)果集)。語(yǔ)法:
1、select * from 表名 [匹配條件];
2、select 列名1[, 列名2, ...] from 表名 [匹配條件];
提示:星號(hào)(*)是選取所有列的通配符。
在終端輸入:insert into persons values (1, 'peter', 'tianjing');
insert into persons values (3, 'bob', 'hebei');
select * from persons;
select * from persons where id=1;
select name from persons;
select name from persons where id=1;
14、in 允許我們?cè)?where 子句中規(guī)定多個(gè)值。匹配條件語(yǔ)法:where 列名 in (列值1, 列值2, ...)例:
1、select * from 表名 where 列名 in (值1, 值2, ...);
2、select 列名1[,列名2,...] from 表名 where列名 in (列值1, 列值2, ...)
在終端輸入:select * from persons where id in (1, 2);
select name from persons where id in (1, 2);
15、and 可在 where 子語(yǔ)句中把兩個(gè)或多個(gè)條件結(jié)合起來(lái)(多個(gè)條件之間是與的關(guān)系)。匹配條件語(yǔ)法:where 列1 = 值1 [and 列2 = 值2 and ...]例:
1、select * from 表名 where 列1 = 值1 [and列2 = 值2 and ...];
2、select 列名1[, 列名2, ...] from 表名where 列1 = 值1 [and 列2 = 值2 and ...];
在終端輸入:select * from persons where id=1 and addr='beijing';
select name from persons where id=1 and addr='beijing';
16、or 可在 where 子語(yǔ)句中把兩個(gè)或多個(gè)條件結(jié)合起來(lái)(多個(gè)條件之間是或的關(guān)系)。匹配條件語(yǔ)法:where 列1 = 值1 [or 列2 = 值2 or ...]例:
1、select * from 表名 where 列1 = 值1 [or 列2 = 值2 or ...];
2、select 列名1[,列名2,...] from 表名 列1 = 值1 [or 列2 = 值2 or ...];
在終端輸入:select * from persons where addr='beijing' or addr='hebei';
17、操作符 between A and B 會(huì)選取介于A、B之間的數(shù)據(jù)范圍。這些值可以是數(shù)值、文本或者日期。匹配條件語(yǔ)法:where 列名 between A and B例:
1、select * from 表名 where 列名 between A and B;
2、select 列名1[,列名2,...] from 表名 where列名 between A and B;
注:匹配字符串時(shí)會(huì)以ascii順序匹配。
在終端輸入:select * from persons where id between 1 and 3;
select * from persons where addr between 'a' and 'c';
18、like 用于模糊查找。匹配條件語(yǔ)法:where 列名 like 列值
1、若列值為數(shù)字相當(dāng)于列名=列值。
2、若列值為字符串可以用通配符“%”代表缺少的字符(一個(gè)或多個(gè))。
在終端輸入:select * from persons where id like 3;
select * from persons where addr like '%jing%';
19、not 可取出原結(jié)果集的補(bǔ)集。匹配條件語(yǔ)法:例:
1、where 列名 not in (列值1, 列值2, ...)
2、 where not (列1 = 值1 [and 列2 = 值2 and...])
3、where not (列1 = 值1 [or 列2 = 值2 or ...])
4、where 列名 not between A and B
5、where 列名 not like 列值
在終端輸入:select * from persons where id not in ( 1 );
select * from persons where addr not like '%jing%';
20、order by 語(yǔ)句根據(jù)指定的列對(duì)結(jié)果集進(jìn)行排序。默認(rèn)按照升序?qū)Y(jié)果集進(jìn)行排序,可使用 desc 關(guān)鍵字按照降序?qū)Y(jié)果集進(jìn)行排序。例:
升序 : select * from 表名 order by 列名;
降序 : select * from 表名 order by 列名 desc;
在終端輸入:select * from persons order by name;
select * from persons order by id;
select * from persons order by id desc;
四、SQLite C語(yǔ)言編程
1、int sqlite3_open(char *db_name,sqlite3 **db);
功能:打開(kāi)數(shù)據(jù)庫(kù)。
參數(shù):db_name:數(shù)據(jù)庫(kù)文件名,若文件名包含ASCII碼表范圍的之外的字符,則其必需是(UTF-8)編碼。
sqlite3:數(shù)據(jù)庫(kù)標(biāo)識(shí),此結(jié)構(gòu)體為數(shù)據(jù)庫(kù)操作句柄。通過(guò)此句柄可對(duì)數(shù)據(jù)庫(kù)文件進(jìn)行相應(yīng)操作。
返回值:成功返回SQLITE_OK,失敗返回非 SQLITE_OK。
2、int sqlite3_close(sqlite3 *db);
功能:關(guān)閉數(shù)據(jù)庫(kù)、釋放打開(kāi)數(shù)據(jù)庫(kù)時(shí)申請(qǐng)的資源。
參數(shù):db:數(shù)據(jù)庫(kù)的標(biāo)識(shí)。
返回值:成功返回 SQLITE_OK。失敗返回非 SQLITE_OK。
注意:sqlite3使用了兩個(gè)庫(kù):pthread、dl,故鏈接時(shí)應(yīng)加上 -lpthread和 -ldl。
3、sqlite3_exec函數(shù):int sqlite3_exec(sqlite3 *db, const char *sql,exechandler_t callback,void *arg, char **errmsg);
功能:執(zhí)行sql指向的SQL語(yǔ)句,若結(jié)果集不為空,函數(shù)會(huì)調(diào)用函數(shù)指針callback所指向的函數(shù)。
參數(shù):db:數(shù)據(jù)庫(kù)的標(biāo)識(shí)。
sql:SQL語(yǔ)句(一條或多條),以’;’結(jié)尾。
callback:是回調(diào)函數(shù)指針,當(dāng)這條語(yǔ)句執(zhí)行之后,sqlite3會(huì)去調(diào)用你提供的這個(gè)函數(shù)。
arg:當(dāng)執(zhí)行sqlite3_exec的時(shí)候傳遞給回調(diào)函數(shù)的參數(shù)。
errmsg:存放錯(cuò)誤信息的地址,執(zhí)行失敗后可以查閱這個(gè)指針。
打印錯(cuò)誤信息方法:printf("%s ", errmsg);
4、回調(diào)函數(shù)指針:typedef int (*exechandler_t)(void *para, int n_column, char **column_value,char **column_name);
功能:此函數(shù)由用戶定義,當(dāng)sqlite3_exec函數(shù)執(zhí)行sql語(yǔ)句后,結(jié)果集不為空時(shí)sqlite3_exec函數(shù)會(huì)自動(dòng)調(diào)用此函數(shù),每次調(diào)用此函數(shù)時(shí)會(huì)把結(jié)果集的一行信息傳給此函數(shù)。
參數(shù):para:sqlite3_exec傳給此函數(shù)的參數(shù),para為任意 數(shù)據(jù)類型的地址。
n_column:結(jié)果集的列數(shù)。
column_value:指針數(shù)組的地址,其存放一行信息中 各個(gè)列值的首地址。
column_name:指針數(shù)組的地址,其存放一行信息中各 個(gè)列值對(duì)應(yīng)列名的首地址。
返回值:若為非0值,則通知sqlite3_exec終止回調(diào)。
5、sqlite3_get_table函數(shù):int sqlite3_get_table(sqlite3 *db, const char *sql,char ***resultp, int *nrow,int *ncolumn,char **errmsg);
功能:執(zhí)行sql指向的SQL語(yǔ)句,函數(shù)將結(jié)果集相關(guān)的數(shù)據(jù)的地址保存在函數(shù)的參數(shù)中。
參數(shù):db:數(shù)據(jù)庫(kù)的標(biāo)識(shí)。
sql:SQL語(yǔ)句(一條或多條),以’;’結(jié)尾。
resultp:指針數(shù)組的地址,其記錄了結(jié)果集的數(shù)據(jù)。內(nèi)存布局:先依次存放各列的列名,然后是每一行各列的值。
nrow:結(jié)果集的行數(shù)(不包含列名)。
ncolumn:結(jié)果集的列數(shù)。
errmsg:錯(cuò)誤信息。
6、sqlite3_free_table函數(shù):void sqlite3_free_table(char **resultp);
功能:釋放sqlite3_get_table分配的內(nèi)存。
參數(shù):結(jié)果集數(shù)據(jù)的首地址。