简体   繁体   English

C,如何传输sqlite3数据库句柄

[英]C, how to transfer sqlite3 database handle

I'm trying to make some utils for using with sqlite databases. 我正在尝试制作一些与sqlite数据库一起使用的工具。 For that I create sqlite3_ut.h and sqlite3_ut.c files 为此,我创建了sqlite3_ut.h和sqlite3_ut.c文件

sqlite_ut.h 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 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 is included in main file. sqlite_ut.h包含在主文件中。

sqlite3 *db;

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

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

Obviously, I can't transfer handle of opened database properly to drop_table function which is of sqlite3 type. 显然,我无法将打开的数据库的句柄正确地传递给sqlite3类型的drop_table函数。

How to do that with suggested program cofiguration? 如何使用建议的程序配置?

SQLite3 handles are of type sqlite3 * , not sqlite3 . SQLite3句柄的类型为sqlite3 * ,而不是sqlite3 Redefine drop_table as follows: 重新定义drop_table如下:

int drop_table(sqlite3 *handle) { … }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM