2012-03-24 29 views
0

我正在尝试使用sqlite数据库使用一些utils。 对于我创建sqlite3_ut.h和sqlite3_ut.c文件C,如何传输sqlite3数据库句柄

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函数。

如何做到这一点与建议的程序配置?

+0

为什么不,你试过了吗?你有错误吗? – 2012-03-24 09:01:33

+0

嘿乔。我在这里有情况。用c头文件和c文件编译C++程序,现在显示'undefined reference to drop_table'消息。该怎么办? – 2012-03-24 09:33:40

回答

2

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

int drop_table(sqlite3 *handle) { … } 
+0

谢谢。我会尽快尝试这个:) – 2012-03-24 09:31:35