gpt4 book ai didi

C、如何传递sqlite3数据库句柄

转载 作者:行者123 更新时间:2023-11-30 14:25:56 25 4
gpt4 key购买 nike

我正在尝试制作一些用于 sqlite 数据库的实用程序。为此,我创建 sqlite3_ut.h 和 sqlite3_ut.c 文件

sqlite_ut.h

#ifndef sqlite3_ut_h
#define sqlite3_ut_h

#include <stdio.h>
#include "sqlite3.h"

int drop_table(sqlite3 handle);

#endif

sqlite_ut.c

int drop_table(sqlite3 handle)
{
int dropped = 0;
printf("Begin Drop\n");

sqlite3_exec(handle, "BEGIN;", NULL, NULL, NULL);
sqlite3_stmt *droptab;
if (sqlite3_prepare_v2(handle, "DROP TABLE mytable;", -1, &droptab, 0) != SQLITE_OK)
printf("db error: %s\n", sqlite3_errmsg(handle));

if(droptab)
{
sqlite3_step(droptab);
dropped = 1;
}
else
printf("Error: drop_table");

sqlite3_finalize(droptab);
sqlite3_exec(handle, "COMMIT;", NULL, NULL, NULL);

printf("End Drop\n");
return dropped;
}

sqlite_ut.h 包含在主文件中。

sqlite3 *db;

int rc = sqlite3_open("m_test.db", &db);
if (rc)...

//error here
int dropped = drop_table(db);

显然,我无法将打开的数据库的句柄正确传输到sqlite3类型的drop_table函数。

如何使用建议的程序配置来做到这一点?

最佳答案

SQLite3 句柄的类型为 sqlite3 *,而不是 sqlite3。重新定义 drop_table 如下:

int drop_table(sqlite3 *handle) { … }

关于C、如何传递sqlite3数据库句柄,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9850370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com