国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQLite > 正文

關于sqlite_exec回調函數中參數傳遞的問題

2024-09-07 00:10:05
字體:
來源:轉載
供稿:網友
上一篇轉載的文章中涉及到了如何用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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰城市| 青州市| 南江县| 余姚市| 峨眉山市| 句容市| 安仁县| 南城县| 临夏市| 珲春市| 红桥区| 政和县| 宁乡县| 遂平县| 龙州县| 河源市| 东安县| 家居| 彭州市| 淄博市| 五原县| 余庆县| 辽源市| 达州市| 健康| 酉阳| 邵武市| 宜州市| 临潭县| 青龙| 北票市| 渝中区| 九寨沟县| 和田县| 朔州市| 巩留县| 上思县| 三都| 舟曲县| 武功县| 连云港市|