2012-03-24 27 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