2014-09-19 117 views
1

我想在C查詢一個SQLite數據庫同步用C

接口使用SQLite我知道存在查詢,

sqlite3_exec 

允許查詢數據只異步(據我發現我的搜索)。

是否有任何方法可以從SQLite數據庫中同步獲得結果到「Select count(*)...」,而無需實現等待數據的內容並將其返回給相同的函數?

我幾乎想要實現返回實際結果它的調用者「howManyItems」的方法...

感謝。

+0

所有SQLite的查詢功能執行同步。你確定你使用官方的C API而不是一些包裝? – 2014-09-20 04:15:21

+0

我實際上不確定運行本身是同步的還是異步的 - 但結果肯定會以回調的方式返回(我將在明天嘗試建議的解決方案),這超出了調用者的範圍,而不是我如何想要得到結果... – evenro 2014-09-20 21:43:58

+2

啊,'sqlite3_exec'接受每行調用的回調。是的,'sqlite3_prepare'函數是你想要的。 – 2014-09-20 21:47:09

回答

4

假設一個開放sqlite3 *db,使用sqlite3_prepare_v2()sqlite3_step()(忽略錯誤檢查爲了簡潔):

sqlite3_stmt *statement = null; 

// prepare our query 
sqlite3_prepare_v2(db, "select count(*) from foo;", -1, &statement, 0); 

// if there were parameters to bind, we'd do that here 

// retrieve the first row (only row, in this case) of the results 
int result = sqlite3_step(statement); 

if (result == SQLITE_ROW) 
{ 
    // retrieve the value of the first column (0-based) 
    int count = sqlite3_column_int(statement, 0); 

    // do something with count 
} 

// free our statement 
sqlite3_finalize(statement);