sqlite 基本回调用法

#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>

	//n_column 是列数, 在select语句中采用了*, 而该表有两列, 所以此值为2 , column_value 和 column_name 应该看成是一维指针, 它指向的是一个
	//char *的字符串
int LoadMyInfo (void *para , int n_column , char ** column_value, char ** column_name){
	int i;
	printf("record %d field \n" , n_column);
	
	for( i = 0; i < n_column; i++){
		printf("field name : %s  field value: %s \n", column_name[i], column_value[i]);
	}
	printf("-------------------------\n");
	return 0;
}

int main(){
	sqlite3 *db;
	int result;
	char *errmsg = NULL ;
	result = sqlite3_open("C://sely.db" , &db);
	if( result != SQLITE_OK){
		return -1;
	}
	
	result = sqlite3_exec(db , "create table MyTable_1(ID integer primary key autoincrement, name varchar(32))", NULL, NULL, &errmsg);
    if(result != SQLITE_OK){
		printf("create table error: %d , reason : %s" , result, errmsg);
	}

	result = sqlite3_exec(db, "insert into MyTable_1 (name)  values(\"walk\")", NULL , NULL , &errmsg);
	if(result != SQLITE_OK){
		printf("create table error: %d , reason : %s" , result, errmsg);
	}

    result = sqlite3_exec(db, "insert into MyTable_1 (name) values(\"bus\")", 0 , 0 , &errmsg);
	if(result != SQLITE_OK){
		printf("create table error: %d , reason : %s" , result, errmsg);
	}

	result = sqlite3_exec(db, "insert into MyTable_1 (name)  values(\"bike\")", NULL , NULL , &errmsg);
	if(result != SQLITE_OK){
		printf("create table error: %d , reason : %s" , result, errmsg);
	}

    result = sqlite3_exec(db, "insert into MyTable_1 (name) values(\"car\")", 0 , 0 , &errmsg);
	if(result != SQLITE_OK){
		printf("create table error: %d , reason : %s" , result, errmsg);
        }
	
	//loadMyInfo 是回调函数, 每次有一个记录被查询出, 都会调用一次, 结果集中有多少记录,就会执行多少次!    
	result = sqlite3_exec(db, "select *from MyTable_1", LoadMyInfo, NULL, &errmsg);
    if(result != SQLITE_OK){
		printf("create table error: %d , reason : %s" , result, errmsg);
	}

	sqlite3_close(db);
	system("pause");
	return 0;
}
/* 执行结果如下:
record 2 field
field name : ID  field value: 1
field name : name  field value: walk
-------------------------
record 2 field
field name : ID  field value: 2
field name : name  field value: bus
-------------------------
record 2 field
field name : ID  field value: 3
field name : name  field value: bike
-------------------------
record 2 field
field name : ID  field value: 4
field name : name  field value: car
-------------------------
Press any key to continue . . .
*/


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLite3 的回调函数是在执行 SQL 语句时,将结果返回给应用程序的一种方式。通过回调函数,我们可以在 SQL 语句执行期间对返回的结果进行处理或者操作。 下面是 SQLite3 回调函数的使用方法: 1. 定义回调函数 回调函数一般会在 SQLite3 执行 SQL 语句时被调用,因此我们需要先定义一个回调函数,它必须符合 SQLite3 规定的特定格式。 例如,我们定义一个回调函数,用于将查询结果打印出来: ```c 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; } ``` 回调函数的参数说明: - `void *NotUsed`:回调函数可以接收一个指针类型的参数,但它并不会被使用,通常可以将其设置为 `NULL`。 - `int argc`:SQL 语句返回的结果集中有多少列。 - `char **argv`:一个包含结果集中每一列的值的字符串数组。 - `char **azColName`:一个包含结果集中每一列的名称的字符串数组。 2. 执行 SQL 语句 在调用 SQLite3 执行 SQL 语句的函数时,我们需要将回调函数作为其中一个参数传递进去。 例如,我们查询一个名为 `users` 的表中所有的数据,将结果通过回调函数输出: ```c sqlite3 *db; char *zErrMsg = 0; int rc; const char* data = "Callback function called"; char *sql = "SELECT * FROM users"; rc = sqlite3_open("test.db", &db); if (rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); return(0); } else { fprintf(stderr, "Opened database successfully\n"); } 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); ``` 在执行 SQL 语句时,我们将回调函数 `callback` 作为第三个参数传递给了 `sqlite3_exec` 函数。此外,我们还可以通过第四个参数将自定义数据传递给回调函数,在回调函数中使用。 总之,SQLite3 的回调函数可以让我们在执行 SQL 语句时自定义处理结果的方式,非常灵活和方便。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值