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

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

[[343746]]

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

数据库

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

no name score
4 一口Linux 89.0

创建表格语句如下:

  1. CREATE TABLE  IF NOT EXISTS student (no integer primary keyname 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<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.  } 

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

no name score
4 一口Linux 77.0
5 一口peng 88.0
6 一口wang 99.0
7 一口网 66.0

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

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

数据库

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

no name score
4 一口Linux 89.0

创建表格语句如下:

  1. CREATE TABLE  IF NOT EXISTS student (no integer primary keyname 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<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.  } 

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

no name score
4 一口Linux 77.0
5 一口peng 88.0
6 一口wang 99.0
7 一口网 66.0

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

 


在这里插入图片描述

 

 

由上图可知:代码中:

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

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

使用回调函数执行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); 

功能:

执行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.  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

回调函数方法实现的代码,需要实现一个回调函数: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 <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; 

运行主页面:

 

插入记录:

显示记录:


删除记录:

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

 

 

责任编辑:武晓燕 来源: 一口Linux
相关推荐

2022-03-24 08:51:48

Redis互联网NoSQL

2024-03-25 08:18:31

2021-02-15 15:40:28

SQLite3数据库

2023-12-21 08:02:21

CPUJava8列表

2024-04-12 12:19:08

语言模型AI

2023-05-22 13:27:17

2021-03-04 00:09:31

MySQL体系架构

2021-02-28 20:53:37

Cookie存储浏览器

2020-09-03 06:35:44

Linux权限文件

2023-09-02 21:27:09

2022-07-15 08:16:56

Stream函数式编程

2023-03-06 21:29:41

mmap技术操作系统

2020-12-07 06:19:50

监控前端用户

2021-07-08 10:08:03

DvaJS前端Dva

2012-03-06 12:59:11

iOS SQLite3iOSSQLite3

2011-08-01 13:32:07

Objective-C Sqlite3 框架

2021-05-06 05:38:48

Python文件操作异常模块

2021-02-22 09:44:03

KubernetesDNSLinux

2011-07-07 16:42:38

iPhone Sqlite3 数据库

2021-03-22 10:05:59

netstat命令Linux
点赞
收藏

51CTO技术栈公众号