自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何用C語言操作sqlite3,一文搞懂

開發(fā) 后端
sqlite3編程接口非常多,對于初學(xué)者來說,我們暫時只需要掌握常用的幾個函數(shù),其他函數(shù)自然就知道如何使用了。

[[343746]]

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

數(shù)據(jù)庫

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

no name score
4 一口Linux 89.0

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

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

常用函數(shù)

sqlite3_open

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

功能:

打開sqlite數(shù)據(jù)庫

參數(shù):

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

db: 指向sqlite句柄的指針,后面對數(shù)據(jù)庫所有的操作都要依賴這個句柄

返回值:

 

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

sqlite3_close

  1. int   sqlite3_close(sqlite3 *db); 

功能:

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

返回值:

 

成功返回0,失敗返回錯誤碼

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

功能:

打印錯誤信息

返回值:

 

返回錯誤信息

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

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ù)庫句柄

sql:SQL語句

resultp:用來指向sql執(zhí)行結(jié)果的指針

nrow:滿足條件的記錄的數(shù)目

ncolumn:每條記錄包含的字段數(shù)目

errmsg:錯誤信息指針的地址

返回值:

 

成功返回0,失敗返回錯誤碼

舉例

下面比如我們要顯示student表中所有的數(shù)據(jù)信息,我們就可以利用sqlite3_get_table()執(zhí)行語句:

  1. select * from student 

實現(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<nrow; i++) 
  13.  { 
  14.   for (j=0; j<ncolumn; 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ù)信息如下:

no name score
4 一口Linux 77.0
5 一口peng 88.0
6 一口wang 99.0
7 一口網(wǎng) 66.0

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

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

數(shù)據(jù)庫

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

no name score
4 一口Linux 89.0

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

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

常用函數(shù)

sqlite3_open

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

功能:

打開sqlite數(shù)據(jù)庫

參數(shù):

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

db: 指向sqlite句柄的指針

返回值:

 

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

sqlite3_close

  1. int   sqlite3_close(sqlite3 *db); 

功能:

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

返回值:

 

成功返回0,失敗返回錯誤碼

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

功能:

打印錯誤信息

返回值:

 

返回錯誤信息

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

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ù)庫句柄

sql:SQL語句

resultp:用來指向sql執(zhí)行結(jié)果的指針

nrow:滿足條件的記錄的數(shù)目

ncolumn:每條記錄包含的字段數(shù)目

errmsg:錯誤信息指針的地址

返回值:

 

成功返回0,失敗返回錯誤碼

舉例

下面比如我們要顯示student表中所有的數(shù)據(jù)信息,我們就可以利用sqlite3_get_table()執(zhí)行語句:

  1. select * from student 

實現(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<nrow; i++) 
  13.  { 
  14.   for (j=0; j<ncolumn; 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ù)信息如下:

no name score
4 一口Linux 77.0
5 一口peng 88.0
6 一口wang 99.0
7 一口網(wǎng) 66.0

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

 


在這里插入圖片描述

 

 

由上圖可知:代碼中:

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

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

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

sqlite3_exec

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

功能:

執(zhí)行SQL操作

參數(shù):

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

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

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

errmsg:錯誤信息指針的地址

返回值:

 

成功返回0,失敗返回錯誤碼

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

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

功能:

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

參數(shù):

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

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

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

f_name:包含每個字段名稱的指針數(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<f_num; 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ù)方法實現(xiàn)的代碼,需要實現(xiàn)一個回調(diào)函數(shù):callback。函數(shù)sqlite3_exec()在解析命令"select * from student" ,沒獲取到一行數(shù)據(jù)就會調(diào)用一次回調(diào)函數(shù), 參考上面的表格student,

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

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

 

運行結(jié)果

編譯需要使用第三方庫lsqlite3。

  1. gcc student.c -o run -lsqlite3 

其他函數(shù)

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

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

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

國際慣例,上完整代碼:

  1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3. #include <unistd.h> 
  4. #include <sqlite3.h> 
  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.  noname, score); 
  20.  #if __DEBUG 
  21.  printf("cmd:%s\n",sqlstr); 
  22.  #endif 
  23.  if (sqlite3_exec(db, sqlstr, NULLNULL, &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, NULLNULL, &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<f_num; 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<nrow; i++) 
  95.  { 
  96.   for (j=0; j<ncolumn; 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; 

運行主頁面:

 

插入記錄:

顯示記錄:


刪除記錄:

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

 

 

責(zé)任編輯:武曉燕 來源: 一口Linux
相關(guān)推薦

2024-04-12 12:19:08

語言模型AI

2023-12-21 08:02:21

CPUJava8列表

2022-03-24 08:51:48

Redis互聯(lián)網(wǎng)NoSQL

2024-03-25 08:18:31

2021-03-22 10:05:59

netstat命令Linux

2023-09-08 08:20:46

ThreadLoca多線程工具

2023-09-15 12:00:01

API應(yīng)用程序接口

2021-05-06 05:38:48

Python文件操作異常模塊

2021-02-22 09:44:03

KubernetesDNSLinux

2021-02-15 15:40:28

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

2019-11-19 08:00:00

神經(jīng)網(wǎng)絡(luò)AI人工智能

2020-03-18 14:00:47

MySQL分區(qū)數(shù)據(jù)庫

2021-01-13 05:21:59

參數(shù)

2021-06-30 08:45:02

內(nèi)存管理面試

2022-06-07 10:13:22

前端沙箱對象

2022-08-15 15:39:23

JavaScript面向?qū)ο?/a>數(shù)據(jù)

2023-04-03 15:04:00

RPCPHP語言

2023-08-24 16:50:45

2023-10-16 08:16:31

Bean接口類型

2024-06-05 11:43:10

點贊
收藏

51CTO技術(shù)棧公眾號