sqlite嵌入式數據庫C語言基本操作(1)
sqlite嵌入式數據庫C語言基本操作(1)
sqlite是應用最廣泛的嵌入式數據庫,沒有之一,其詳細的介紹參見官方網站(http://sqlite.org).最新的版本是3.12.2.
C語言的API函數列表見官網SQLite C Interface.這里對一些基本的函數做一個簡單的說明。
sqlite3_open: 打開數據庫文件.
sqlite3_close: 關閉數據庫。
sqlite3_prepare : 編譯SQL,准備語句。
sqlite3_step: sqlite3_stmt的方式執行SQL語句。
sqlite3_exec: 執行SQL語句。
sqlite3_finalize: 釋放 sqlite3_stmt資源。
sqlite3_bind_int: 綁定SQL語句中int參數。
sqlite3_bind_text: 綁定SQL語句中的字符串參數。
sqlite3_bind_blob: 綁定SQL語句中的二進制參數。
sqlite3_column_int: 遍歷數據集,得到一行數據中某列的int值。
sqlite3_column_text: 遍歷數據集,得到一行數據某列的字符串值。
sqlite3_column_blob: 遍歷數據集,得到一行數據某列的二進制值。
……
以下這個例子做一個簡單的insert,和 select 的例子,來講sqlite3數據庫的簡單數據操作。
首先建一張簡單的表,並插入初始化數據。
create table userinfo ( userid integer, username char(32), PRIMARY KEY (userid) );
insert into userinfo (userid,username) values (1,"Alex");
insert into userinfo (userid,username) values (2,"Allan");
insert into userinfo (userid,username) values (3,"Blizzard");
insert into userinfo (userid,username) values (4,"Bob");
建立對應的數據模型.
typedef struct userinfo_s {
int userid; //用戶編號
char username[33]; //用戶姓名
struct userinfo_s * next; //下一個用戶
} userinfo_t;
得到所有Users的函數
int get_all_userinfos(userinfo_t u){
char sql[512] = "select * from userinfo;"; //sql語句
sqlite3 * db = NULL;
sqlite3_stmt * stmt = NULL;
int ret_open = sqlite3_open("test.db",&db); //打開數據庫文件
if (ret_open != SQLITE_OK){
printf("open test.db fail\n");
return -1;
}
int ret = sqlite3_prepare(db,sql,-1,&stmt,NULL); //准備好SQL語句
if (ret != SQLITE_OK){
printf("prepare fail \n");
return ret;
}
ret = sqlite3_step(stmt); //執行SQL
int count = 0;
if(ret != SQLITE_ROW){ //返回有數據
return 0;
}else{
userinfo_t * p = u; //建立鏈表
do{
userinfo_t * user = calloc(sizeof(userinfo_t),1);
user->userid = sqlite3_column_int(stmt,0); //得到USerid 注意get的初始值為index為0
const char * name = sqlite3_column_text(stmt,1); //得到用戶名
if (name){
int len = strlen(name);
strncpy(user->username,name,len);
}
user->next = NULL;
p->next = user; //串聯 建立一個單項鏈表
p = p->next;
count ++;
}while((ret = sqlite3_step(stmt))==SQLITE_ROW); //取值下一個
}
sqlite3_finalize(stmt); //釋放資源 stmt
sqlite3_close(db); //關閉數據庫句柄
return count;
}
insert操作函數:
int insert_userinfo_t(userinfo_t * u){
char sql[512] = "insert into userinfo (userid,username) values (?,?)"; //插入數據庫語句
sqlite3 *db = NULL;
sqlite3_stmt * stmt = NULL;
int ret_open = sqlite3_open("test.db",&db); //打開數據庫
if(ret_open !=SQLITE_OK){
printf("open test.db fail\n");
return -1;
}
int ret = sqlite3_prepare(db,sql,-1,&stmt,NULL); //准備語句
if (ret != SQLITE_OK){
printf("prepare fail \n");
return ret;
}
sqlite3_bind_int(stmt,1,u->userid); //綁定參數, 注意綁定參數的初始index值為1
sqlite3_bind_text(stmt,2,u->username,32,NULL);
ret = sqlite3_step(stmt); //執行語句
if(ret == SQLITE_DONE){ //執行結果
ret = SQLITE_OK;
}
sqlite3_finalize(stmt); //釋放資源
sqlite3_close(db); //關閉數據庫
return ret;
}
測試代碼:
void test_get_all_userinfos(){
userinfo_t u;
int count = get_all_userinfos(&u);
userinfo_t * p = u.next;
userinfo_t * q = p;
printf("count:%d\n",count);
while(p){
printf("userid:%d username:%s \n",p->userid,p->username);
p = p->next;
}
free_userinfo_t(&u);
}
void test_insert_into_userinfo_t(){
userinfo_t new_user;
new_user.userid = 5;
char new_name[33]= "micheal";
strncpy(new_user.username,new_name,32);
int ret= insert_userinfo_t(&new_user);
printf("ret = %d\n",ret);
}
輸出
count:4
userid:1 username:Alex
userid:2 username:Allan
userid:3 username:Blizzard
userid:4 username:Bob
ret = 1
count:5
userid:1 username:Alex
userid:2 username:Allan
userid:3 username:Blizzard
userid:4 username:Bob
userid:5 username:micheal
gcc編譯最后記得加上 -lsqlite3
本文只是很初略的介紹了sqlite嵌入式數據庫的基本操作,初略到甚至例程只講了兩個操作,增加和查詢,修改和刪除都沒有講到,不過看了增加操作,修改和刪除操作就很容易實現了。
寫這篇文章目的只是一個想講一個過程,一個操作數據庫的基本過程
一個數據庫操作流程
1 打開數據庫
2 編譯准備SQL語句
3 綁定SQL語句參數
4 執行SQL語句
5 關閉釋放Statement
6 關閉數據庫
當然也只是一個初略的流程,對於數據庫操作失敗的流程處理也沒有。不過了解上面這個初略的流程就可以了。更深入的講解請參見下篇:[sqlite嵌入式數據庫C語言基本操作(2)]
參考文獻:
sqlite官方網站:sqlite.org
generated by haroopad
