|
|
|
|
|
| 关于sqlite_exec回调函数中参数传递的问题 |
|
|
作者:tamsyn
来源:www.sqlite.com.cn
时间:2007-12-18
【 字体:大 中 小 】 〖 双击滚屏 〗 |
|
|
|
|
上一篇转载的文章中涉及到了如何用C来作回调函数读取或写入SQLITE数据库的问题,但其中没有关于回调函数如何作参数传递的问题,比如想要在你的主调函数中获取该变量,就需要通过调用sqlite3_exec函数给回调函数传递结构体指针,下面我作了一例: #include <stdio.h> #include <stdlib.h> #include <sqlite3.h>
struct olt_info { int olt_index; int onu_on_line; int ui_port1; int ui_port2; int ui_port3; int ui_port4; };
int my_callback(void * olt_temp, int argc, char * value[], char * name[]) { int i; struct olt_info * pdata = NULL;
pdata = (struct olt_info *)olt_temp; puts("Here below is the code line:\n"); for (i = 0; i < argc; i++) { printf("%s == %s\n", name[i], value[i]); } puts("Code line over.\n"); pdata->olt_index = (int)atoi(value[0]); pdata->onu_on_line = (int)atoi(value[1]); pdata->ui_port1 = (int)atoi(value[2]); pdata->ui_port2 = (int)atoi(value[3]); pdata->ui_port3 = (int)atoi(value[4]); pdata->ui_port4 = (int)atoi(value[5]); return 0; }
int main(int argc, char * argv[]) { sqlite3 * olt_db = NULL; int rc = 0; int i; char * err_msg = NULL; char temp_msg[150]; struct olt_info * olt_temp= (struct olt_info *)malloc(sizeof(struct olt_info)); rc = sqlite3_open("olt.db", &olt_db); if (rc) { fprintf(stderr, "Open database error, %s\n", sqlite3_errmsg(olt_db)); exit(1); } else { fprintf(stdout, "Open database OK.\n"); }
rc = sqlite3_exec(olt_db, "create table olt_tbl(olt_index integer primary key autoincrement, onu_on_line smallint, ui_port1 smallint, ui_port2 smallint, ui_port3 smallint, ui_port4 smallint);", NULL, NULL, &err_msg);
if (rc != SQLITE_OK) { fprintf(stderr, "Create table error, %s\n", err_msg); exit(1); } else { fprintf(stdout, "Create table OK.\n"); }
for (i = 0; i < 6; i++) { sprintf(temp_msg, "insert into olt_tbl(onu_on_line, ui_port1, ui_port2, ui_port3, ui_port4) values(%d, %d, %d, %d, %d)", i * 16, i, i, i, i); //rc = sqlite3_exec(olt_db, "insert into olt_tbl(onu_on_line, ui_port1, ui_port2, ui_port3, ui_port4) values(32, 1, 1, 1, 1);", NULL, NULL, &err_msg); rc = sqlite3_exec(olt_db, temp_msg, NULL, NULL, &err_msg); }
if (rc != SQLITE_OK) { fprintf(stderr, "Insert items failure, %s\n", err_msg); } else { fprintf(stdout, "Insert items OK.\n"); }
rc = sqlite3_exec(olt_db, "select * from olt_tbl where olt_index==4;", my_callback, olt_temp, &err_msg); if (rc != SQLITE_OK) { fprintf(stderr, "Selete from olt_tbl failure, %s\n", err_msg); exit(1); } else { fprintf(stdout, "Excute sql OK.\n"); }
printf("%d-%d-%d-%d-%d-%d\n", olt_temp->olt_index, olt_temp->onu_on_line,olt_temp->ui_port1,olt_temp->ui_port2,olt_temp->ui_port3,olt_temp->ui_port4);
free(olt_temp);
sqlite3_close(olt_db); return 0; }
其中my_callback(void * pdata, int argc, char * value[], char *name[])为回调函数,切记回调函数只能按照这种格式来定义参数, 第一个参数为你的主调函数传递过来的指针, 第二个参数为变量的个数, 第三个为变量的值, 第四个为变量的名称, 有两个问题需要注意: 一、这里面参数都是字符串类型,根据您的需要作出强制类型转换即可。 二、第一个参数为void *类型,需要在你的回调函数里强制转换成需要的类型。
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1914908
|
|
|
| 浏览次数: 【 打 印 】【 关 闭 】 |
上一篇:用sqlite 执行标准 sql 语法
下一篇:android.database.sqlite 文档翻译(1)
|
|
|
|
|
|
|
|
| 论坛登陆 |
| 文章搜索 |
|
| 推荐文章 |
|
| 酷站推荐 |
|
|
| 热门文章 |
|
| 网站统计 |
|
|
|