关于sqlite_exec回调函数中参数传递的问题

    上一篇转载的文章中涉及到了如何用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 *类型,需要在你的回调函数里强制转换成需要的类型。
 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值