sqlite3基本操作

2021-05-28 07:45:02 字數 3551 閱讀 6515

sqlite3對很多通過的sql語句都支援,像select,update,insert,delete等等都支援地很好,只要懂sql語句就可以用sqlite3。

1,下面是幾個比較重要的api函式:

/* 開啟資料庫,如果不存在則建立乙個 */

int sqlite3_open(const char*, sqlite3**); //個人感覺這裡只是開啟乙個檔案, sqlite3對資料庫的操作就是對檔案的操作,意思是在這裡只要檔案存在它就會返回乙個值,而不管這個檔案是不是sqlite型別的。即這裡的sqlite3** 返回的資料庫控制代碼,即使不為空,也不一定是資料庫的檔案.這個問題有待測試。

/* 關閉資料庫 */

int sqlite3_close(sqlite3*);

/* 獲取錯誤資訊字串 */

const char *sqlite3_errmsg(sqlite3*);

/* 執行sql語句 */

int sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void*, char**);

/* 獲取sql查詢結果,其實這個函式是不推薦使用的,一方面是因為它不安全,另一方面它不能處理blog型別的字段,當然,在這裡我在查詢的時候還是使用了它,因為沒有涉及到多複雜多龐大的資料,這個使用起來又簡單,所以就選用了這個方法 */

int sqlite3_get_table(

sqlite3 *db, /* an open database */

const char *zsql, /* sql to be evaluated */

char ***pazresult, /* results of the query */

int *pnrow, /* number of result rows written here */

int *pncolumn, /* number of result columns written here */

char **pzerrmsg /* error msg written here */

);還有複雜一些的api如下,我沒仔細看,像sqlite3_get_table這些函式就是由下面這些api組合而成的

int sqlite3_prepare(sqlite3*, const char*, int, sqlite3_stmt**, const char**);//解析sql語句

int sqlite3_bind_double(sqlite3_stmt*, int, double);//關聯某些值

int sqlite3_bind_int(sqlite3_stmt*, int, int);

int sqlite3_bind_int64(sqlite3_stmt*, int, long long int);

int sqlite3_bind_null(sqlite3_stmt*, int);

int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));

int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int n, void(*)(void*));

int sqlite3_step(sqlite3_stmt*);

2,建立自增長型別的字段

create table history (id integer primary key autoincrement,

name text,userid text,message text,

updatetime text,issend integer);

這個語句中建立的id欄位即為自增長的字段,在插入記錄的時候只需把id欄位寫為null即可實現其自增長,如:

insert into history values(null,"levin","123456","hello sqlite3","2010-10-23",1);

3.sqlite3的限制讀取條數

/* 在其它sql資料庫中select指定條數的記錄用如下語句:*/

select top 10 * from history;

/* 而在sqlite3中使用下面語句:*/

select * from history limit 10

4.sqlite3的日期函式

sqlite3的日期函式還是很強大的,列舉幾個常用的。

/* 獲取當前時間 */

select datetime('now')

輸出:2010-10-23 12:10:50

/* 獲取當前時間偏移 */

select datetime('now','+20 days');

輸出:2010-11-12 12:12:54

/* 獲取今年開始時間 */

select datetime('now','start of year');

輸出:2010-01-01 00:00:00

/* 獲取本月開始時間 */

select datetime('now','start of month');

輸出:2010-10-01 00:00:00

/* 獲取今天開始時間 */

select datetime('now','start of day');

輸出:2010-10-23 00:00:00

另外,sqlite3裡面有個很重要的時間函式,strftime,這個跟posix裡面的strftime函式很像,也是將日期型別格式化為字串型別,如:

select strftime('%y|%m|%d','now');

2010|10|23

它的格式化字元和posix的strftime也完全一樣,再例如我要查詢本月的聊天記錄,可以使用下面語句:

select * from history where

strftime('%y',updatetime) == strftime('%y','now') and

strftime('%m',updatetime) == strftime('%m','now') ;

5.寫入較大資料時採用事務提高效率

我在應用sqlite3的時候其實只是寫入了少量的資料,剛開始覺得效率不是什麼大問題,後來有使用者反饋說他的290+好友的飛訊號在登入時要向磁碟寫入幾十s的資料,這個效率問題著實需要改善,於是採用事務來處理寫入,事務的使用也非常簡單,其實也就是下面的語句:

sqlite3_exec(db, "begin transaction;", 0,0, &errmsg);

for(;;)

sqlite3_exec(db, "commit transaction;", 0, 0, &errmsg);

這樣一系列地插入語句就可以被作為乙個事務來執行,在commit transaction的時候將插入操作寫入磁碟,避免了每次插入記錄時頻繁地讀寫磁碟資料庫,從而使效率大大提高,據說可以比單純地插入快1000倍,這個我無從考證,不過我這裡確實快了很多,幾百條記錄可以瞬間寫入。

sqlite3基本操作

1.sqlite3 db.sqlite3 進入名為db.sqlite3的資料庫 2.sqlite tables 檢視所有表 3 sqlite schema 表名 檢視表結構 4 sqlite database 檢視目前掛載的資料庫 5 sqlite quit 退出資料庫 6 root wzz sql...

sqlite3 基本操作

安裝sqlite3 sudo dnf install sqlite3 fedora sudo apt get install sqlite3 ubuntu 1 開啟資料庫,如果沒有則建立 sqlite3 test.db 2 建立 格式,student 中 有 integer型的 id 作為主鍵,不能...

sqlite3基本操作

一 終端操作 1.先進入專案的目錄裡 找到documents路徑 nslog nshomedirectory 終端 cd documents 2.建立資料庫 sqlite3 資料庫名稱 字尾可以隨便加 sqlite3 db student.sql 4.建立表 sql語句 create table i...