電子產(chǎn)業(yè)一站式賦能平臺(tái)

PCB聯(lián)盟網(wǎng)

搜索
查看: 31|回復(fù): 0
收起左側(cè)

如何用C語(yǔ)言操作sqlite3,一文搞懂

[復(fù)制鏈接]

248

主題

248

帖子

1437

積分

三級(jí)會(huì)員

Rank: 3Rank: 3

積分
1437
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 昨天 11:50 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
點(diǎn)擊左上方藍(lán)色“一口Linux”,選擇“設(shè)為星標(biāo)
第一時(shí)間看干貨文章
?【干貨】嵌入式驅(qū)動(dòng)工程師學(xué)習(xí)路線?【干貨】Linux嵌入式知識(shí)點(diǎn)-思維導(dǎo)圖-免費(fèi)獲取?【就業(yè)】一個(gè)可以寫(xiě)到簡(jiǎn)歷的基于Linux物聯(lián)網(wǎng)綜合項(xiàng)目?【就業(yè)】找工作簡(jiǎn)歷模版




嵌入式數(shù)據(jù)庫(kù)sqlite3【基礎(chǔ)篇】-基本命令操作,小白一看就懂

嵌入式數(shù)據(jù)庫(kù)sqlite3【進(jìn)階篇】-子句和函數(shù)的使用,小白一文入門
sqlite3編程接口非常多,對(duì)于初學(xué)者來(lái)說(shuō),我們暫時(shí)只需要掌握常用的幾個(gè)函數(shù),其他函數(shù)自然就知道如何使用了。
數(shù)據(jù)庫(kù)本篇假設(shè)數(shù)據(jù)庫(kù)為my.db,有數(shù)據(jù)表student。
nonamescore4一口Linux89.0創(chuàng)建表格語(yǔ)句如下:
CREATE TABLE  IF NOT EXISTS student (no integer primary key, name text, score real);
常用函數(shù)sqlite3_openint   sqlite3_open(char  *path,   sqlite3 **db);
功能:
    打開(kāi)sqlite數(shù)據(jù)庫(kù)
參數(shù):
path: 數(shù)據(jù)庫(kù)文件路徑
db: 指向sqlite句柄的指針,后面對(duì)數(shù)據(jù)庫(kù)所有的操作都要依賴這個(gè)句柄
返回值:
成功返回0,失敗返回錯(cuò)誤碼(非零值)
sqlite3_closeint   sqlite3_close(sqlite3 *db);
功能:
關(guān)閉sqlite數(shù)據(jù)庫(kù)      
返回值:
成功返回0,失敗返回錯(cuò)誤碼
const  char  *sqlite3_errmsg(sqlite3 *db);
功能:
打印錯(cuò)誤信息        
返回值:
返回錯(cuò)誤信息
不使用回調(diào)函數(shù)執(zhí)行SQL語(yǔ)句sqlite3_get_table
int   sqlite3_get_table(sqlite3 *db, const  char  *sql,  char ***resultp,  int*nrow,  int *ncolumn, char **errmsg);
功能:
執(zhí)行SQL操作
參數(shù):
db:數(shù)據(jù)庫(kù)句柄
sql:SQL語(yǔ)句
resultp:用來(lái)指向sql執(zhí)行結(jié)果的指針
nrow:滿足條件的記錄的數(shù)目
ncolumn:每條記錄包含的字段數(shù)目
errmsg:錯(cuò)誤信息指針的地址
返回值:
成功返回0,失敗返回錯(cuò)誤碼
舉例下面比如我們要顯示student表中所有的數(shù)據(jù)信息,我們就可以利用sqlite3_get_table()執(zhí)行語(yǔ)句:
select * from student
實(shí)現(xiàn)代碼如下:
void do_show_sample(sqlite3 *db)
{
  char **result, *errmsg;
int nrow, ncolumn, i, j, index;
if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0)
{
  printf("error : %s
", errmsg);
  sqlite3_free(errmsg);
}
index = ncolumn;
for (i=0; ifor (j=0; jprintf("%-8s : %-8s
", result[j], result[index]);   
   index++;
  }
  printf("************************
");
}
sqlite3_free_table(result);
return;
}
假定當(dāng)前的表格的數(shù)據(jù)信息如下:
nonamescore4一口Linux77.05一口peng88.06一口wang99.07一口網(wǎng)66.0關(guān)于這個(gè)函數(shù)中出現(xiàn)的這些參數(shù)的具體含義,我們可以見(jiàn)下圖:
sqlite3編程接口非常多,對(duì)于初學(xué)者來(lái)說(shuō),我們暫時(shí)只需要掌握常用的幾個(gè)函數(shù),其他函數(shù)自然就知道如何使用了。
數(shù)據(jù)庫(kù)本篇假設(shè)數(shù)據(jù)庫(kù)為my.db,有數(shù)據(jù)表student。
nonamescore4一口Linux89.0創(chuàng)建表格語(yǔ)句如下:
CREATE TABLE  IF NOT EXISTS student (no integer primary key, name text, score real);
常用函數(shù)sqlite3_openint   sqlite3_open(char  *path,   sqlite3 **db);
功能:
    打開(kāi)sqlite數(shù)據(jù)庫(kù)
參數(shù):
path: 數(shù)據(jù)庫(kù)文件路徑
db: 指向sqlite句柄的指針
返回值:
成功返回0,失敗返回錯(cuò)誤碼(非零值)
sqlite3_closeint   sqlite3_close(sqlite3 *db);
功能:
關(guān)閉sqlite數(shù)據(jù)庫(kù)      
返回值:
成功返回0,失敗返回錯(cuò)誤碼
const  char  *sqlite3_errmsg(sqlite3 *db);
功能:
打印錯(cuò)誤信息        
返回值:
返回錯(cuò)誤信息
不使用回調(diào)函數(shù)執(zhí)行SQL語(yǔ)句sqlite3_get_table
int   sqlite3_get_table(sqlite3 *db, const  char  *sql,  char ***resultp,  int*nrow,  int *ncolumn, char **errmsg);
功能:
執(zhí)行SQL操作
參數(shù):
db:數(shù)據(jù)庫(kù)句柄
sql:SQL語(yǔ)句
resultp:用來(lái)指向sql執(zhí)行結(jié)果的指針
nrow:滿足條件的記錄的數(shù)目
ncolumn:每條記錄包含的字段數(shù)目
errmsg:錯(cuò)誤信息指針的地址
返回值:
成功返回0,失敗返回錯(cuò)誤碼
舉例下面比如我們要顯示student表中所有的數(shù)據(jù)信息,我們就可以利用sqlite3_get_table()執(zhí)行語(yǔ)句:
select * from student
實(shí)現(xiàn)代碼如下:
void do_show_sample(sqlite3 *db)
{
  char **result, *errmsg;
int nrow, ncolumn, i, j, index;
if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0)
{
  printf("error : %s
", errmsg);
  sqlite3_free(errmsg);
}
index = ncolumn;
for (i=0; ifor (j=0; jprintf("%-8s : %-8s
", result[j], result[index]);   
   index++;
  }
  printf("************************
");
}
sqlite3_free_table(result);
return;
}
假定當(dāng)前的表格的數(shù)據(jù)信息如下:
nonamescore4一口Linux77.05一口peng88.06一口wang99.07一口網(wǎng)66.0關(guān)于這個(gè)函數(shù)中出現(xiàn)的這些參數(shù)的具體含義,我們可以見(jiàn)下圖:

發(fā)表回復(fù)

本版積分規(guī)則


聯(lián)系客服 關(guān)注微信 下載APP 返回頂部 返回列表