當前位置:編程學習大全網 - 熱門推薦 - 如何使用SQLite,Android上SQLite的最佳實踐

如何使用SQLite,Android上SQLite的最佳實踐

SQLite3是目前最新的SQLite版本。可以從網站上下載SQLite3的源代碼(本書使用的版本是sqlite-3.6.12.tar.gz)。

解壓縮後進入sqlite-3.6.12的根目錄,首先命令“./configure”生成Makefile文件,接著運行命令“make”對源代碼進行編譯,最後運行命令“make install”安裝SQLite3。安裝完畢後,可以運行命令sqlite3查看SQLite3是否能正常運行,如下所示:

[root@localhost ~]# sqlite3

SQLite version 3.6.12

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite>

可以看到,SQLite3啟動後會停留在提示符sqlite>處,等待用戶輸入SQL語句。

在使用SQLite3前需要先了解下SQLite3支持的數據類型。SQLite3支持的基本數據類型主要有以下幾類:

NULL

NUMERIC

INTEGER

REAL

TEXT

SQLite3會自動把其他數據類型轉換成以上5類基本數據類型,轉換規則如下所示:

char、clob、test、varchar—> TEXT

integer—>INTEGER

real、double、float—> REAL

blob—>NULL

其余數據類型都轉變成NUMERIC

下面通過壹個實例來演示SQLite3的使用方法。

新建壹個數據庫

新建數據庫test.db(使用.db後綴是為了標識數據庫文件)。在test.db中新建壹個表test_table,該表具有name,、sex、age三列。SQLite3的具體操作如下所示:

[root@localhost home]# sqlite3 test.db

SQLite version 3.6.12

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite> create table test_table(name, sex, age);

如果數據庫test.db已經存在,則命令“sqlite3 test.db”會在當前目錄下打開test.db。如果數據庫test.db不存在,則命令“sqlite3 test.db”會在當前目錄下新建數據庫test.db。為了提高效率,SQLite3並不會馬上創建test.db,而是等到第壹個表創建完成後才會在物理上創建數據庫。

由於SQLite3能根據插入數據的實際類型動態改變列的類型,所以在create語句中並不要求給出列的類型。

創建索引

為了加快表的查詢速度,往往在主鍵上添加索引。如下所示的是在name列上添加索引的過程。

sqlite> create index test_index on test_table(name);

操作數據

如下所示的是在test_table中進行數據的插入、更新、刪除操作:

sqlite> insert into test_table values ('xiaoming', 'male', 20);

sqlite> insert into test_table values ('xiaohong', 'female', 18);

sqlite> select * from test_table;

xiaoming|male|20

xiaohong|female|18

sqlite> update test_table set age=19 where name = 'xiaohong';

sqlite> select * from test_table;

xiaoming|male|20

xiaohong|female|19

sqlite> delete from test_table where name = 'xiaoming';

sqlite> select * from test_table;

xiaohong|female|19

批量操作數據庫

如下所示的是在test_table中連續插入兩條記錄:

sqlite> begin;

sqlite> insert into test_table values ('xiaoxue', 'female', 18);

sqlite> insert into test_table values ('xiaoliu', 'male', 20);

sqlite> commit;

sqlite> select * from test_table;

xiaohong|female|19

xiaoxue|male|18

xiaoliu|male|20

運行命令commit後,才會把插入的數據寫入數據庫中。

數據庫的導入導出

如下所示的是把test.db導出到sql文件中:

[root@localhost home]# sqlite3 test.db ".dump" > test.sql;

test.sql文件的內容如下所示:

BEGIN TRANSACTION;

CREATE TABLE test_table(name, sex, age);

INSERT INTO "test_table" VALUES('xiaohong','female',19);

CREATE INDEX test_index on test_table(name);

COMMIT;

如下所示的是導入test.sql文件(導入前刪除原有的test.db):

[root@localhost home]# sqlite3 test.db < test.sql;

通過對test.sql文件的導入導出,可以實現數據庫文件的備份。

11.2.2 SQLite3的C接口

以上介紹的是SQLite3數據庫的命令操作方式。在實際使用中,壹般都是應用程序需要對數據庫進行訪問。為此,SQLite3提供了各種編程語言的使用接口(本書介紹C語言接口)。SQLite3具有幾十個C接口,下面介紹壹些常用的C接口。

sqlite_open

作用:打開SQLite3數據庫

原型:int sqlite3_open(const char *dbname, sqlite3 **db)

參數:

dbname:數據庫的名稱;

db:數據庫的句柄;

sqlite_colse

作用:關閉SQLite3數據庫

原型:int sqlite_close(sqlite3 *db)

例如:

test.c:

#include <stdio.h>

#include <sqlite3.h>

static sqlite3 *db=NULL;

int main()

{

int rc;

rc= sqlite3_open("test.db", &db);

if(rc)

{

printf("can't open database!\n");

}

else

{

printf("open database success!\n");

}

sqlite3_close(db);

return 0;

}

運行命令“gcc –o test test.c –lsqlite3”進行編譯,運行test的結果如下所示:

[root@localhost home]# open database success!

sqlite_exec

作用:執行SQL語句

原型:int sqlite3_exec(sqlite3 *db, const char *sql, int (*callback)(void*,int,char**,char**), void *, char **errmsg)

參數:

db:數據庫;

sql:SQL語句;

callback:回滾;

errmsg:錯誤信息

例如:

test.c:

#include <stdio.h>

#include <sqlite3.h>

static sqlite3 *db=NULL;

static char *errmsg=NULL;

int main()

{

int rc;

rc = sqlite3_open("test.db", &db);

rc = sqlite3_exec(db,"insert into test_table values('daobao', 'male', 24)", 0, 0, &errmsg);

if(rc)

{

printf("exec fail!\n");

}

else

{

printf("exec success!\n");

}

sqlite3_close(db);

return 0;

}

編譯完成後,運行test的結果如下所示:

[root@localhost home]# ./test

exec success!

[root@localhost home]# sqlite3 test.db

SQLite version 3.6.11

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite> select * from test_table;

daobao|male|24

sqlite3_get_table

作用:執行SQL查詢

原型:int sqlite3_get_table(sqlite3 *db, const char *zSql, char ***pazResult, int *pnRow, int *pnColumn, char **pzErrmsg)

參數:

db:數據庫;

zSql:SQL語句;

pazResult:查詢結果集;

pnRow:結果集的行數;

pnColumn:結果集的列數;

errmsg:錯誤信息;

sqlite3_free_table

作用:註銷結果集

原型:void sqlite3_free_table(char **result)

參數:

result:結果集;

例如:

test.c:

#include <stdio.h>

#include <sqlite3.h>

static sqlite3 *db=NULL;

static char **Result=NULL;

static char *errmsg=NULL;

int main()

{

int rc, i, j;

int nrow;

int ncolumn;

rc= sqlite3_open("test.db", &db);

rc= sqlite3_get_table(db, "select * from test_table", &Result, &nrow, &ncolumn,

&errmsg);

if(rc)

{

printf("query fail!\n");

}

else

{

printf("query success!\n");

for(i = 1; i <= nrow; i++)

{

for(j = 0; j < ncolumn; j++)

{

printf("%s | ", Result[i * ncolumn + j]);

}

printf("\n");

}

}

sqlite3_free_table(Result);

sqlite3_close(db);

return 0;

}

編譯完成後,運行test的結果如下所示:

[root@localhost home]# ./test

query success!

xiaohong | female | 19 |

xiaoxue | female | 18 |

xiaoliu | male | 20 |

daobao | male | 24 |

sqlite3_prepare

作用:把SQL語句編譯成字節碼,由後面的執行函數去執行

原型:int sqlite3_prepare(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **stmt, const char **pTail)

參數:

db:數據庫;

zSql:SQL語句;

nByte:SQL語句的最大字節數;

stmt:Statement句柄;

pTail:SQL語句無用部分的指針;

sqlite3_step

作用:步步執行SQL語句字節碼

原型:int sqlite3_step (sqlite3_stmt *)

例如:

test.c:

#include <stdio.h>

#include <sqlite3.h>

static sqlite3 *db=NULL;

static sqlite3_stmt *stmt=NULL;

int main()

{

int rc, i, j;

int ncolumn;

rc= sqlite3_open("test.db", &db);

rc=sqlite3_prepare(db,"select * from test_table",-1,&stmt,0);

if(rc)

{

printf("query fail!\n");

}

else

{

printf("query success!\n");

rc=sqlite3_step(stmt);

ncolumn=sqlite3_column_count(stmt);

while(rc==SQLITE_ROW)

{

for(i=0; i<2; i++)

{

printf("%s | ", sqlite3_column_text(stmt,i));

}

printf("\n");

rc=sqlite3_step(stmt);

}

}

sqlite3_finalize(stmt);

sqlite3_close(db);

return 0;

}

編譯完成後,運行test的結果如下所示:

[root@localhost home]# ./test

query success!

xiaohong | female | 19 |

xiaoxue | female | 18 |

xiaoliu | male | 20 |

daobao | male | 24 |

在程序中訪問SQLite3數據庫時,要註意C API的接口定義和數據類型是否正確,否則會得到錯誤的訪問結果。

  • 上一篇:山西萬榮十大景點
  • 下一篇:個人取向劇情
  • copyright 2024編程學習大全網