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

 sqlite3编程接口非常多,对于初学者来说,我们暂时只需要掌握常用的几个函数,其他函数自然就知道如何使用了。

目前创新互联建站已为成百上千的企业提供了网站建设、域名、雅安服务器托管、网站托管维护、企业网站设计、岱山网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

数据库

本篇假设数据库为my.db,有数据表student。

nonamescore
4一口Linux89.0

创建表格语句如下:

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

常用函数

sqlite3_open

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

功能:

打开sqlite数据库

参数:

path: 数据库文件路径

db: 指向sqlite句柄的指针,后面对数据库所有的操作都要依赖这个句柄

返回值:

成功返回0,失败返回错误码(非零值)

sqlite3_close

 
 
 
 
  1. int   sqlite3_close(sqlite3 *db); 

功能:

关闭sqlite数据库

返回值:

成功返回0,失败返回错误码

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

功能:

打印错误信息

返回值:

返回错误信息

不使用回调函数执行SQL语句

sqlite3_get_table

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

功能:

执行SQL操作

参数:

db:数据库句柄

sql:SQL语句

resultp:用来指向sql执行结果的指针

nrow:满足条件的记录的数目

ncolumn:每条记录包含的字段数目

errmsg:错误信息指针的地址

返回值:

成功返回0,失败返回错误码

举例

下面比如我们要显示student表中所有的数据信息,我们就可以利用sqlite3_get_table()执行语句:

 
 
 
 
  1. select * from student 

实现代码如下:

 
 
 
 
  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.  } 

假定当前的表格的数据信息如下:

nonamescore
4一口Linux77.0
5一口peng88.0
6一口wang99.0
7一口网66.0

关于这个函数中出现的这些参数的具体含义,我们可以见下图:

sqlite3编程接口非常多,对于初学者来说,我们暂时只需要掌握常用的几个函数,其他函数自然就知道如何使用了。

数据库

本篇假设数据库为my.db,有数据表student。

nonamescore
4一口Linux89.0

创建表格语句如下:

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

常用函数

sqlite3_open

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

功能:

打开sqlite数据库

参数:

path: 数据库文件路径

db: 指向sqlite句柄的指针

返回值:

成功返回0,失败返回错误码(非零值)

sqlite3_close

 
 
 
 
  1. int   sqlite3_close(sqlite3 *db); 

功能:

关闭sqlite数据库

返回值:

成功返回0,失败返回错误码

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

功能:

打印错误信息

返回值:

返回错误信息

不使用回调函数执行SQL语句

sqlite3_get_table

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

功能:

执行SQL操作

参数:

db:数据库句柄

sql:SQL语句

resultp:用来指向sql执行结果的指针

nrow:满足条件的记录的数目

ncolumn:每条记录包含的字段数目

errmsg:错误信息指针的地址

返回值:

成功返回0,失败返回错误码

举例

下面比如我们要显示student表中所有的数据信息,我们就可以利用sqlite3_get_table()执行语句:

 
 
 
 
  1. select * from student 

实现代码如下:

 
 
 
 
  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.  } 

假定当前的表格的数据信息如下:

nonamescore
4一口Linux77.0
5一口peng88.0
6一口wang99.0
7一口网66.0

关于这个函数中出现的这些参数的具体含义,我们可以见下图:

在这里插入图片描述

由上图可知:代码中:

 
 
 
 
  1. ncolumn = 3 
  2. nrow    = 5 
  3. result 指向所有的结果组成的字符串数组, 
  4. 各个具体字符串的下标,图上已经标明。 

结合此图再去理解代码,就很容易理解代码的实现原理。

使用回调函数执行SQL语句

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); 

功能:

执行SQL操作

参数:

db:数据库句柄

sql:SQL语句,就是我们前面两章用于操作表的增删改查语句

callback:回调函数

errmsg:错误信息指针的地址

返回值:

成功返回0,失败返回错误码

回调函数

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

功能:

每找到一条记录自动执行一次回调函数

参数:

para:传递给回调函数的参数

f_num:记录中包含的字段数目

f_value:包含每个字段值的指针数组

f_name:包含每个字段名称的指针数组

返回值:

成功返回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. { 
  6.  int i; 
  7.  
  8.  for (i=0; i
  9.  { 
  10.   printf("%-8s", f_val[i]); 
  11.  } 
  12.  printf("\n"); 
  13.  
  14.  return 0; 
  15. } 
  16.  
  17. void do_show(sqlite3 *db) 
  18. { 
  19.  char *errmsg; 
  20.  
  21.  printf("no      name    score\n"); 
  22.   
  23.  if (sqlite3_exec(db, "select * from student", callback, NULL, &errmsg) != 0) 
  24.  { 
  25.   printf("error : %s\n", sqlite3_errmsg(db)); 
  26.  } 
  27.  printf("\n"); 
  28.  
  29.  return; 
  30. } 

回调函数方法实现的代码,需要实现一个回调函数:callback。函数sqlite3_exec()在解析命令"select * from student" ,没获取到一行数据就会调用一次回调函数, 参考上面的表格student,

 
 
 
 
  1. callback()总共会被调用5次, 
  2. f_num 对应结果的列数,为3 
  3. f_value 则指向 每一列对应的值组成的字符串数组 

假设现在callback是第四次被调用,如下图:

运行结果

编译需要使用第三方库lsqlite3。

 
 
 
 
  1. gcc student.c -o run -lsqlite3 

其他函数

 
 
 
 
  1. sqlite3 *pdb, 数据库句柄,跟文件句柄FILE很类似 
  2. sqlite3_stmt *stmt, 这个相当于ODBC的Command对象,用于保存编译好的SQL语句 
  3.  
  4. sqlite3_exec(), 执行非查询的sql语句 
  5. sqlite3_prepare(), 准备sql语句,执行select语句或者要使用parameter bind时,用这个函数(封装了sqlite3_exec) 
  6. Sqlite3_step(), 在调用sqlite3_prepare后,使用这个函数在记录集中移动 

还有一系列的函数,用于从记录集字段中获取数据,如

 
 
 
 
  1. sqlite3_column_text(), 取text类型的数据 
  2. sqlite3_column_blob(),取blob类型的数据 
  3. sqlite3_column_int(), 取int类型的数据 

国际惯例,上完整代码:

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

运行主页面:

插入记录:

显示记录:


删除记录:

本文转载自微信公众号「一口Linux」,可以通过以下二维码关注。转载本文请联系一口Linux公众号。


网站题目:如何用C语言操作sqlite3,一文搞懂
标题URL:http://kmvly.com/article/coicose.html