一江春水向東流

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

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

          上一篇轉載的文章中涉及到了如何用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

          ?

          posted on 2008-05-10 16:32 allic 閱讀(3746) 評論(0)  編輯  收藏 所屬分類: 開源數據庫學習研究
          主站蜘蛛池模板: 蒙山县| 高邑县| 南澳县| 宁国市| 镶黄旗| 玛曲县| 西充县| 庆元县| 板桥市| 大新县| 龙里县| 青河县| 南郑县| 盐津县| 津南区| 康乐县| 扶绥县| 阳新县| 山西省| 古田县| 巴彦淖尔市| 循化| 登封市| 浙江省| 上思县| 陇西县| 普定县| 依兰县| 仁寿县| 建昌县| 贡嘎县| 剑川县| 嘉峪关市| 清苑县| 德州市| 新田县| 巴中市| 化州市| 九龙县| 宽甸| 外汇|