sqlite c/c++ 编程接口
1. 安装
在 c/c++ 程序中使用 sqlite 之前,我们需要确保机器上已经有 sqlite 库。可以查看 sqlite 安装章节了解安装过程。
2. c/c++ 接口 api
以下是重要的 c&c++ / sqlite 接口程序,可以满足您在 c/c++ 程序中使用 sqlite 数据库的需求。如果您需要了解更多细节,请查看 sqlite 官方文档。
序号 | api & 描述 |
---|---|
1 | sqlite3_open(const char *filename, sqlite3 **ppdb)
该例程打开一个指向 sqlite 数据库文件的连接,返回一个用于其他 sqlite 程序的数据库连接对象。 如果 filename 参数是 null 或 ':memory:',那么 sqlite3_open() 将会在 ram 中创建一个内存数据库,这只会在 session 的有效时间内持续。 如果文件名 filename 不为 null,那么 sqlite3_open() 将使用这个参数值尝试打开数据库文件。如果该名称的文件不存在,sqlite3_open() 将创建一个新的命名为该名称的数据库文件并打开。 |
2 | sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg)
该例程提供了一个执行 sql 命令的快捷方式,sql 命令由 sql 参数提供,可以由多个 sql 命令组成。 在这里,第一个参数 sqlite3 是打开的数据库对象,sqlite_callback 是一个回调,data 作为其第一个参数,errmsg 将被返回用来获取程序生成的任何错误。 sqlite3_exec() 程序解析并执行由 sql 参数所给的每个命令,直到字符串结束或者遇到错误为止。 |
3 | sqlite3_close(sqlite3*)
该例程关闭之前调用 sqlite3_open() 打开的数据库连接。所有与连接相关的语句都应在连接关闭之前完成。 如果还有查询没有完成,sqlite3_close() 将返回 sqlite_busy 禁止关闭的错误消息。 |
3. 连接数据库
下面的 c 代码段显示了如何连接到一个现有的数据库。如果数据库不存在,那么它就会被创建,最后将返回一个数据库对象。
#include <stdio.h> #include <sqlite3.h> int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "opened database successfully\n"); } sqlite3_close(db); }
现在,让我们来编译和运行上面的程序,在当前目录中创建我们的数据库 test.db。您可以根据需要改变路径。
$gcc test.c -l sqlite3 $./a.out opened database successfully
如果要使用 c++ 源代码,可以按照下列所示编译代码:
$g++ test.c -l sqlite3
在这里,把我们的程序链接上 sqlite3 库,以便向 c 程序提供必要的函数。这将在您的目录下创建一个数据库文件 test.db,您将得到如下结果:
-rwxr-xr-x. 1 root root 7383 may 8 02:06 a.out -rw-r--r--. 1 root root 323 may 8 02:05 test.c -rw-r--r--. 1 root root 0 may 8 02:06 test.db
4. 创建表
下面的 c 代码段将用于在先前创建的数据库中创建一个表:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *notused, int argc, char **argv, char **azcolname){ int i; for(i=0; i<argc; i++){ printf("%s = %s\n", azcolname[i], argv[i] ? argv[i] : "null"); } printf("\n"); return 0; } int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; char *sql; /* open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stdout, "opened database successfully\n"); } /* create sql statement */ sql = "create table company(" \ "id int primary key not null," \ "name text not null," \ "age int not null," \ "address char(50)," \ "salary real );"; /* execute sql statement */ rc = sqlite3_exec(db, sql, callback, 0, &zerrmsg); if( rc != sqlite_ok ){ fprintf(stderr, "sql error: %s\n", zerrmsg); sqlite3_free(zerrmsg); }else{ fprintf(stdout, "table created successfully\n"); } sqlite3_close(db); return 0; }
上述程序编译和执行时,它会在 test.db 文件中创建 company 表,最终文件列表如下所示:
-rwxr-xr-x. 1 root root 9567 may 8 02:31 a.out -rw-r--r--. 1 root root 1207 may 8 02:31 test.c -rw-r--r--. 1 root root 3072 may 8 02:31 test.db
5. insert 操作
下面的 c 代码段显示了如何在上面创建的 company 表中创建记录:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *notused, int argc, char **argv, char **azcolname){ int i; for(i=0; i<argc; i++){ printf("%s = %s\n", azcolname[i], argv[i] ? argv[i] : "null"); } printf("\n"); return 0; } int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; char *sql; /* open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "opened database successfully\n"); } /* create sql statement */ sql = "insert into company (id,name,age,address,salary) " \ "values (1, 'paul', 32, 'california', 20000.00 ); " \ "insert into company (id,name,age,address,salary) " \ "values (2, 'allen', 25, 'texas', 15000.00 ); " \ "insert into company (id,name,age,address,salary)" \ "values (3, 'teddy', 23, 'norway', 20000.00 );" \ "insert into company (id,name,age,address,salary)" \ "values (4, 'mark', 25, 'rich-mond ', 65000.00 );"; /* execute sql statement */ rc = sqlite3_exec(db, sql, callback, 0, &zerrmsg); if( rc != sqlite_ok ){ fprintf(stderr, "sql error: %s\n", zerrmsg); sqlite3_free(zerrmsg); }else{ fprintf(stdout, "records created successfully\n"); } sqlite3_close(db); return 0; }
上述程序编译和执行时,它会在 company 表中创建给定记录,并会显示以下两行:
opened database successfully records created successfully
6. select 操作
在我们开始讲解获取记录的范例之前,让我们先了解下回调函数的一些细节,这将在我们的范例使用到。这个回调提供了一个从 select 语句获得结果的方式。它声明如下:
typedef int (*sqlite3_callback)( void*, /* data provided in the 4th argument of sqlite3_exec() */ int, /* the number of columns in row */ char**, /* an array of strings representing fields in the row */ char** /* an array of strings representing column names */ );
如果上面的回调在 sqlite_exec() 程序中作为第三个参数,那么 sqlite 将为 sql 参数内执行的每个 select 语句中处理的每个记录调用这个回调函数。
下面的 c 代码段显示了如何从前面创建的 company 表中获取并显示记录:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *data, int argc, char **argv, char **azcolname){ int i; fprintf(stderr, "%s: ", (const char*)data); for(i=0; i<argc; i++){ printf("%s = %s\n", azcolname[i], argv[i] ? argv[i] : "null"); } printf("\n"); return 0; } int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; char *sql; const char* data = "callback function called"; /* open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "opened database successfully\n"); } /* create sql statement */ sql = "select * from company"; /* execute sql statement */ rc = sqlite3_exec(db, sql, callback, (void*)data, &zerrmsg); if( rc != sqlite_ok ){ fprintf(stderr, "sql error: %s\n", zerrmsg); sqlite3_free(zerrmsg); }else{ fprintf(stdout, "operation done successfully\n"); } sqlite3_close(db); return 0; }
上述程序编译和执行时,它会产生以下结果:
opened database successfully callback function called: id = 1 name = paul age = 32 address = california salary = 20000.0 callback function called: id = 2 name = allen age = 25 address = texas salary = 15000.0 callback function called: id = 3 name = teddy age = 23 address = norway salary = 20000.0 callback function called: id = 4 name = mark age = 25 address = rich-mond salary = 65000.0 operation done successfully
7. update 操作
下面的 c 代码段显示了如何使用 update 语句来更新任何记录,然后从 company 表中获取并显示更新的记录:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *data, int argc, char **argv, char **azcolname){ int i; fprintf(stderr, "%s: ", (const char*)data); for(i=0; i<argc; i++){ printf("%s = %s\n", azcolname[i], argv[i] ? argv[i] : "null"); } printf("\n"); return 0; } int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; char *sql; const char* data = "callback function called"; /* open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "opened database successfully\n"); } /* create merged sql statement */ sql = "update company set salary = 25000.00 where id=1; " \ "select * from company"; /* execute sql statement */ rc = sqlite3_exec(db, sql, callback, (void*)data, &zerrmsg); if( rc != sqlite_ok ){ fprintf(stderr, "sql error: %s\n", zerrmsg); sqlite3_free(zerrmsg); }else{ fprintf(stdout, "operation done successfully\n"); } sqlite3_close(db); return 0; }
上述程序编译和执行时,它会产生以下结果:
opened database successfully callback function called: id = 1 name = paul age = 32 address = california salary = 25000.0 callback function called: id = 2 name = allen age = 25 address = texas salary = 15000.0 callback function called: id = 3 name = teddy age = 23 address = norway salary = 20000.0 callback function called: id = 4 name = mark age = 25 address = rich-mond salary = 65000.0 operation done successfully
8. delete 操作
下面的 c 代码段显示了如何使用 delete 语句删除任何记录,然后从 company 表中获取并显示剩余的记录:
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h> static int callback(void *data, int argc, char **argv, char **azcolname){ int i; fprintf(stderr, "%s: ", (const char*)data); for(i=0; i<argc; i++){ printf("%s = %s\n", azcolname[i], argv[i] ? argv[i] : "null"); } printf("\n"); return 0; } int main(int argc, char* argv[]) { sqlite3 *db; char *zerrmsg = 0; int rc; char *sql; const char* data = "callback function called"; /* open database */ rc = sqlite3_open("test.db", &db); if( rc ){ fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stderr, "opened database successfully\n"); } /* create merged sql statement */ sql = "delete from company where id=2; " \ "select * from company"; /* execute sql statement */ rc = sqlite3_exec(db, sql, callback, (void*)data, &zerrmsg); if( rc != sqlite_ok ){ fprintf(stderr, "sql error: %s\n", zerrmsg); sqlite3_free(zerrmsg); }else{ fprintf(stdout, "operation done successfully\n"); } sqlite3_close(db); return 0; }
上述程序编译和执行时,它会产生以下结果:
opened database successfully callback function called: id = 1 name = paul age = 32 address = california salary = 20000.0 callback function called: id = 3 name = teddy age = 23 address = norway salary = 20000.0 callback function called: id = 4 name = mark age = 25 address = rich-mond salary = 65000.0 operation done successfully