日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
如何用C語(yǔ)言操作sqlite3,一文搞懂

 sqlite3編程接口非常多,對(duì)于初學(xué)者來(lái)說(shuō),我們暫時(shí)只需要掌握常用的幾個(gè)函數(shù),其他函數(shù)自然就知道如何使用了。

目前創(chuàng)新互聯(lián)建站已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、岱山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

數(shù)據(jù)庫(kù)

本篇假設(shè)數(shù)據(jù)庫(kù)為my.db,有數(shù)據(jù)表student。

nonamescore
4一口Linux89.0

創(chuàng)建表格語(yǔ)句如下:

 
 
 
 
  1. CREATE TABLE  IF NOT EXISTS student (no integer primary key, name text, score real); 

常用函數(shù)

sqlite3_open

 
 
 
 
  1. int   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_close

 
 
 
 
  1. int   sqlite3_close(sqlite3 *db); 

功能:

關(guān)閉sqlite數(shù)據(jù)庫(kù)

返回值:

成功返回0,失敗返回錯(cuò)誤碼

 
 
 
 
  1. const  char  *sqlite3_errmsg(sqlite3 *db); 

功能:

打印錯(cuò)誤信息

返回值:

返回錯(cuò)誤信息

不使用回調(diào)函數(shù)執(zhí)行SQL語(yǔ)句

sqlite3_get_table

 
 
 
 
  1. int   sqlite3_get_table(sqlite3 *db, const  char  *sql,  char ***resultp,  int*nrow,  int *ncolumn, char  

功能:

執(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ǔ)句:

 
 
 
 
  1. select * from student 

實(shí)現(xiàn)代碼如下:

 
 
 
 
  1. void do_show_sample(sqlite3 *db) 
  2.  { 
  3.   char **result, *errmsg; 
  4.  int nrow, ncolumn, i, j, index; 
  5.  
  6.  if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0) 
  7.  { 
  8.   printf("error : %s\n", errmsg); 
  9.   sqlite3_free(errmsg); 
  10.  } 
  11.  index = ncolumn; 
  12.  for (i=0; i
  13.  { 
  14.   for (j=0; j
  15.   { 
  16.    printf("%-8s : %-8s\n", result[j], result[index]);    
  17.    index++; 
  18.   } 
  19.   printf("************************\n"); 
  20.  } 
  21.  sqlite3_free_table(result); 
  22.  return; 
  23.  } 

假定當(dāng)前的表格的數(shù)據(jù)信息如下:

nonamescore
4一口Linux77.0
5一口peng88.0
6一口wang99.0
7一口網(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。

nonamescore
4一口Linux89.0

創(chuàng)建表格語(yǔ)句如下:

 
 
 
 
  1. CREATE TABLE  IF NOT EXISTS student (no integer primary key, name text, score real); 

常用函數(shù)

sqlite3_open

 
 
 
 
  1. int   sqlite3_open(char  *path,   sqlite3 **db); 

功能:

打開(kāi)sqlite數(shù)據(jù)庫(kù)

參數(shù):

path: 數(shù)據(jù)庫(kù)文件路徑

db: 指向sqlite句柄的指針

返回值:

成功返回0,失敗返回錯(cuò)誤碼(非零值)

sqlite3_close

 
 
 
 
  1. int   sqlite3_close(sqlite3 *db); 

功能:

關(guān)閉sqlite數(shù)據(jù)庫(kù)

返回值:

成功返回0,失敗返回錯(cuò)誤碼

 
 
 
 
  1. const  char  *sqlite3_errmsg(sqlite3 *db); 

功能:

打印錯(cuò)誤信息

返回值:

返回錯(cuò)誤信息

不使用回調(diào)函數(shù)執(zhí)行SQL語(yǔ)句

sqlite3_get_table

 
 
 
 
  1. int   sqlite3_get_table(sqlite3 *db, const  char  *sql,  char ***resultp,  int*nrow,  int *ncolumn, char * 

功能:

執(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ǔ)句:

 
 
 
 
  1. select * from student 

實(shí)現(xiàn)代碼如下:

 
 
 
 
  1. void do_show_sample(sqlite3 *db) 
  2.  { 
  3.   char **result, *errmsg; 
  4.  int nrow, ncolumn, i, j, index; 
  5.  
  6.  if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0) 
  7.  { 
  8.   printf("error : %s\n", errmsg); 
  9.   sqlite3_free(errmsg); 
  10.  } 
  11.  index = ncolumn; 
  12.  for (i=0; i
  13.  { 
  14.   for (j=0; j
  15.   { 
  16.    printf("%-8s : %-8s\n", result[j], result[index]);    
  17.    index++; 
  18.   } 
  19.   printf("************************\n"); 
  20.  } 
  21.  sqlite3_free_table(result); 
  22.  return; 
  23.  } 

假定當(dāng)前的表格的數(shù)據(jù)信息如下:

nonamescore
4一口Linux77.0
5一口peng88.0
6一口wang99.0
7一口網(wǎng)66.0

關(guān)于這個(gè)函數(shù)中出現(xiàn)的這些參數(shù)的具體含義,我們可以見(jiàn)下圖:

在這里插入圖片描述

由上圖可知:代碼中:

 
 
 
 
  1. ncolumn = 3 
  2. nrow    = 5 
  3. result 指向所有的結(jié)果組成的字符串?dāng)?shù)組, 
  4. 各個(gè)具體字符串的下標(biāo),圖上已經(jīng)標(biāo)明。 

結(jié)合此圖再去理解代碼,就很容易理解代碼的實(shí)現(xiàn)原理。

使用回調(diào)函數(shù)執(zhí)行SQL語(yǔ)句

sqlite3_exec

 
 
 
 
  1. typedef  int (*sqlite3_callback)(void *, int, char **, char **); 
  2.  
  3. int   sqlite3_exec(sqlite3 *db, const  char  *sql,  sqlite3_callback callback, void *,  char **errmsg); 

功能:

執(zhí)行SQL操作

參數(shù):

db:數(shù)據(jù)庫(kù)句柄

sql:SQL語(yǔ)句,就是我們前面兩章用于操作表的增刪改查語(yǔ)句

callback:回調(diào)函數(shù)

errmsg:錯(cuò)誤信息指針的地址

返回值:

成功返回0,失敗返回錯(cuò)誤碼

回調(diào)函數(shù)

 
 
 
 
  1. typedef  int (*sqlite3_callback)(void *para, int f_num, char **f_value, char **f_name); 

功能:

每找到一條記錄自動(dòng)執(zhí)行一次回調(diào)函數(shù)

參數(shù):

para:傳遞給回調(diào)函數(shù)的參數(shù)

f_num:記錄中包含的字段數(shù)目

f_value:包含每個(gè)字段值的指針數(shù)組

f_name:包含每個(gè)字段名稱的指針數(shù)組

返回值:

成功返回0,失敗返回-1

舉例

 
 
 
 
  1. sqlite3 *db; 
  2. char  *errmsg,**resultp; 
  3.  
  4. int callback(void *para, int f_num, char **f_val, char **f_name) 
  5.  int i; 
  6.  
  7.  for (i=0; i
  8.  { 
  9.   printf("%-8s", f_val[i]); 
  10.  } 
  11.  printf("\n"); 
  12.  
  13.  return 0; 
  14.  
  15. void do_show(sqlite3 *db) 
  16.  char *errmsg; 
  17.  
  18.  printf("no      name    score\n"); 
  19.   
  20.  if (sqlite3_exec(db, "select * from student", callback, NULL, &errmsg) != 0) 
  21.  { 
  22.   printf("error : %s\n", sqlite3_errmsg(db)); 
  23.  } 
  24.  printf("\n"); 
  25.  
  26.  return; 

回調(diào)函數(shù)方法實(shí)現(xiàn)的代碼,需要實(shí)現(xiàn)一個(gè)回調(diào)函數(shù):callback。函數(shù)sqlite3_exec()在解析命令"select * from student" ,沒(méi)獲取到一行數(shù)據(jù)就會(huì)調(diào)用一次回調(diào)函數(shù), 參考上面的表格student,

 
 
 
 
  1. callback()總共會(huì)被調(diào)用5次, 
  2. f_num 對(duì)應(yīng)結(jié)果的列數(shù),為3 
  3. f_value 則指向 每一列對(duì)應(yīng)的值組成的字符串?dāng)?shù)組 

假設(shè)現(xiàn)在callback是第四次被調(diào)用,如下圖:

運(yùn)行結(jié)果

編譯需要使用第三方庫(kù)lsqlite3。

 
 
 
 
  1. gcc student.c -o run -lsqlite3 

其他函數(shù)

 
 
 
 
  1. sqlite3 *pdb, 數(shù)據(jù)庫(kù)句柄,跟文件句柄FILE很類似 
  2. sqlite3_stmt *stmt, 這個(gè)相當(dāng)于ODBC的Command對(duì)象,用于保存編譯好的SQL語(yǔ)句 
  3.  
  4. sqlite3_exec(), 執(zhí)行非查詢的sql語(yǔ)句 
  5. sqlite3_prepare(), 準(zhǔn)備sql語(yǔ)句,執(zhí)行select語(yǔ)句或者要使用parameter bind時(shí),用這個(gè)函數(shù)(封裝了sqlite3_exec) 
  6. Sqlite3_step(), 在調(diào)用sqlite3_prepare后,使用這個(gè)函數(shù)在記錄集中移動(dòng) 

還有一系列的函數(shù),用于從記錄集字段中獲取數(shù)據(jù),如

 
 
 
 
  1. sqlite3_column_text(), 取text類型的數(shù)據(jù) 
  2. sqlite3_column_blob(),取blob類型的數(shù)據(jù) 
  3. sqlite3_column_int(), 取int類型的數(shù)據(jù) 

國(guó)際慣例,上完整代碼:

 
 
 
 
  1. #include  
  2. #include  
  3. #include  
  4. #include  
  5.  
  6. void do_insert(sqlite3 *db) 
  7.  int no; 
  8.  char name[16]; 
  9.  float score; 
  10.  char sqlstr[128], *errmsg; 
  11.  
  12.  printf("input no : "); 
  13.  scanf("%d", &no); 
  14.  printf("input name : "); 
  15.  scanf("%s", name); 
  16.  printf("input score : "); 
  17.  scanf("%f", &score); 
  18.  sprintf(sqlstr, "insert into student values (%d, '%s', %.1f)",  
  19.  no, name, score); 
  20.  #if __DEBUG 
  21.  printf("cmd:%s\n",sqlstr); 
  22.  #endif 
  23.  if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != 0) 
  24.  { 
  25.   printf("error : %s\n", sqlite3_errmsg(db)); 
  26.  } 
  27.  else 
  28.  { 
  29.   printf("insert is done\n"); 
  30.  } 
  31.  printf("\n"); 
  32.  
  33.  return; 
  34.  
  35. void do_delete(sqlite3 *db) 
  36.  char *errmsg; 
  37.  char sqlstr[128], expression[64]; 
  38.  
  39.  printf("input expression : "); 
  40.  scanf("%s", expression);//name='ma' 
  41.  sprintf(sqlstr, "delete from student where %s", expression); 
  42. #if __DEBUG 
  43.  printf("cmd:%s\n",sqlstr); 
  44. #endif 
  45.  if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != 0) 
  46.  { 
  47.   printf("error : %s\n", sqlite3_errmsg(db)); 
  48.  } 
  49.  else 
  50.  { 
  51.   printf("deletet is done\n"); 
  52.  } 
  53.  printf("\n"); 
  54.  
  55.  return; 
  56.   
  57. int callback(void *para, int f_num, char **f_val, char **f_name) 
  58.  int i; 
  59.  
  60.  for (i=0; i
  61.  { 
  62.   printf("%-8s", f_val[i]); 
  63.  } 
  64.  printf("\n"); 
  65.  
  66.  return 0; 
  67.  
  68. void do_show(sqlite3 *db) 
  69.  char *errmsg; 
  70.  
  71.  printf("no      name    score\n"); 
  72.  
  73.  if (sqlite3_exec(db, "select * from student", callback, NULL, &errmsg) != 0) 
  74.  { 
  75.   printf("error : %s\n", sqlite3_errmsg(db)); 
  76.  } 
  77.  printf("\n"); 
  78.  
  79.  return; 
  80.  
  81.  void do_show_sample(sqlite3 *db) 
  82.  { 
  83.   char **result, *errmsg; 
  84.  int nrow, ncolumn, i, j, index; 
  85.  
  86.  if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0) 
  87.  { 
  88.   printf("error : %s\n", errmsg); 
  89.   sqlite3_free(errmsg); 
  90.  } 
  91.   
  92.  index = ncolumn; 
  93.  
  94.  for (i=0; i
  95.  { 
  96.   for (j=0; j
  97.   { 
  98.    printf("%-8s : %-8s\n", result[j], result[index]); 
  99.     
  100.      
  101.    index++; 
  102.   } 
  103.   printf("************************\n"); 
  104.  } 
  105.  sqlite3_free_table(result); 
  106.  
  107.  return; 
  108.  } 
  109.   
  110.  
  111. int main() 
  112.  sqlite3 *db; 
  113.  int n; 
  114.  char clean[64]; 
  115.  
  116.  if (sqlite3_open("my.db", &db) < 0) 
  117.  { 
  118.   printf("fail to sqlite3_open : %s\n", sqlite3_errmsg(db)); 
  119.   return -1; 
  120.  } 
  121.  
  122.  while ( 1 ) 
  123.  { 
  124.   printf("*********************************************\n"); 
  125.   printf("1: insert record   \n2: delete record  \n3: show record  \n4: quit\n"); 
  126.   printf("*********************************************\n"); 
  127.   printf("please select : ");  
  128.    
  129.   if (scanf("%d", &n) != 1) 
  130.   { 
  131.    fgets(clean, 64, stdin); 
  132.    printf("\n"); 
  133.    continue; 
  134.   } 
  135.   switch ( n ) 
  136.   { 
  137.    case 1 : 
  138.     do_insert(db); 
  139.     break; 
  140.    case 2 : 
  141.     do_delete(db); 
  142.     break; 
  143.    case 3 : 
  144.     do_show_sample(db); 
  145.     break; 
  146.    case 4 : 
  147.     sqlite3_close(db); 
  148.     exit(0); 
  149.   } 
  150.  } 
  151.  return 0; 

運(yùn)行主頁(yè)面:

插入記錄:

顯示記錄:


刪除記錄:

本文轉(zhuǎn)載自微信公眾號(hào)「一口Linux」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系一口Linux公眾號(hào)。


文章標(biāo)題:如何用C語(yǔ)言操作sqlite3,一文搞懂
URL標(biāo)題:http://www.5511xx.com/article/coicose.html