一江春水向東流

          做一個有思想的人,期待與每一位熱愛思考的人交流,您的關(guān)注是對我最大的支持。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            44 隨筆 :: 139 文章 :: 81 評論 :: 0 Trackbacks

          上一篇轉(zhuǎn)載的文章中涉及到了如何用C來作回調(diào)函數(shù)讀取或?qū)懭隨QLITE數(shù)據(jù)庫的問題,但其中沒有關(guān)于回調(diào)函數(shù)如何作參數(shù)傳遞的問題,比如想要在你的主調(diào)函數(shù)中獲取該變量,就需要通過調(diào)用sqlite3_exec函數(shù)給回調(diào)函數(shù)傳遞結(jié)構(gòu)體指針,下面我作了一例:
          ????
          #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[])為回調(diào)函數(shù),切記回調(diào)函數(shù)只能按照這種格式來定義參數(shù),
          ??? 第一個參數(shù)為你的主調(diào)函數(shù)傳遞過來的指針,
          ??? 第二個參數(shù)為變量的個數(shù),
          ??? 第三個為變量的值,
          ??? 第四個為變量的名稱,
          ??? 有兩個問題需要注意:
          ??????? 一、這里面參數(shù)都是字符串類型,根據(jù)您的需要作出強(qiáng)制類型轉(zhuǎn)換即可。
          ??????? 二、第一個參數(shù)為void *類型,需要在你的回調(diào)函數(shù)里強(qiáng)制轉(zhuǎn)換成需要的類型。
          ?


          Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1914908

          ?

          posted on 2008-05-10 16:32 allic 閱讀(3752) 評論(0)  編輯  收藏 所屬分類: 開源數(shù)據(jù)庫學(xué)習(xí)研究
          主站蜘蛛池模板: 榆林市| 昌宁县| 保定市| 宝兴县| 峨边| 禹州市| 县级市| 孟津县| 宝丰县| 宁乡县| 天全县| 丽江市| 南木林县| 奉贤区| 滦南县| 丹凤县| 南岸区| 濉溪县| 曲阳县| 阿瓦提县| 南溪县| 长汀县| 阿尔山市| 西青区| 壤塘县| 乌兰察布市| 西乌珠穆沁旗| 平原县| 平阳县| 疏勒县| 尚义县| 林州市| 衡山县| 葵青区| 大同县| 车险| 肥乡县| 鸡泽县| 积石山| 景宁| 敖汉旗|