2012-06-01 83 views
11

我試圖創建在C++中使用sqlite3的lib ..我得到錯誤sqlite3_prepare_v2'
數據庫如圖logcat的沒有在此範圍
聲明。C++:用於插入創建使用SQLite數據庫和更新

日誌文件

..\src\Test.cpp: In function 'int main(int, const char**)': 
..\src\Test.cpp:21:85: error: 'sqlite3_prepare_v2' was not declared in this scope 
..\src\Test.cpp:30:13: error: variable 'sqlite3 in' has initializer but incomplete type 
..\src\Test.cpp:30:30: error: invalid use of incomplete type 'sqlite3 {aka struct sqlite3}' 
..\src\/sqlite3.h:73:16: error: forward declaration of 'sqlite3 {aka struct sqlite3}' 

這裏是我的代碼

#include <iostream> 
using namespace std; 
#include "sqlite3.h" 

int main (int argc, const char * argv[]) { 

sqlite3 *db; 
sqlite3_open("test.db", & db); 

    string createQuery = "CREATE TABLE IF NOT EXISTS items (busid INTEGER PRIMARY KEY, ipaddr TEXT, time TEXT NOT NULL DEFAULT (NOW()));"; 
    sqlite3_stmt *createStmt; 
    cout << "Creating Table Statement" << endl; 
    sqlite3_prepare_v2(db, createQuery.c_str(), createQuery.size(), &createStmt, NULL); 
    cout << "Stepping Table Statement" << endl; 
    if (sqlite3_step(createStmt) != SQLITE_DONE) cout << "Didn't Create Table!" << endl; 

    string insertQuery = "INSERT INTO items (time, ipaddr) VALUES ('test', '192.168.1.1');"; // WORKS! 


    sqlite3_stmt *insertStmt; 
    cout << "Creating Insert Statement" << endl; 
    sqlite3_prepare(db, insertQuery.c_str(), insertQuery.size(), &insertStmt, NULL); 
    cout << "Stepping Insert Statement" << endl; 
    if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl; 

cout << "Success!" << endl; 

return 0; 
} 

請幫助我。謝謝.....

回答

16
#include <sqlite3.h> 

應該包含sqlite3_prepare_v2和struct sqlite3。確保你包含正確的sqlite3.h文件。

另外在sqlite3_prepare_v2中,第三個參數可以是(也應該是你的情況)-1,所以sql被讀到第一個空終止符。

工作使用SQLite 3.7.11裸機樣本:

#include <sqlite3.h> 
int test() 
{ 
    sqlite3* pDb = NULL; 
    sqlite3_stmt* query = NULL; 
    int ret = 0; 
    do // avoid nested if's 
    { 
     // initialize engine 
     if (SQLITE_OK != (ret = sqlite3_initialize())) 
     { 
      printf("Failed to initialize library: %d\n", ret); 
      break; 
     } 
     // open connection to a DB 
     if (SQLITE_OK != (ret = sqlite3_open_v2("test.db", &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL))) 
     { 
      printf("Failed to open conn: %d\n", ret); 
      break; 
     } 
     // prepare the statement 
     if (SQLITE_OK != (ret = sqlite3_prepare_v2(pDb, "SELECT 2012", -1, &query, NULL))) 
     { 
      printf("Failed to prepare insert: %d, %s\n", ret, sqlite3_errmsg(pDb)); 
      break; 
     } 
     // step to 1st row of data 
     if (SQLITE_ROW != (ret = sqlite3_step(query))) // see documentation, this can return more values as success 
     { 
      printf("Failed to step: %d, %s\n", ret, sqlite3_errmsg(pDb)); 
      break; 
     } 
     // ... and print the value of column 0 (expect 2012 here) 
     printf("Value from sqlite: %s", sqlite3_column_text(query, 0));  

    } while (false); 
    // cleanup 
    if (NULL != query) sqlite3_finalize(query); 
    if (NULL != pDb) sqlite3_close(pDb); 
    sqlite3_shutdown(); 
    return ret; 
} 

希望這有助於

+1

謝謝克里斯蒂安.. – Satyam

+0

非常有用我們thnx哥們... –

+0

在這裏。數據庫打開...但我需要創建數據庫 –

1

通過this鏈接。我不確定。它可能會幫助你。

我認爲他們是在sqlite3.h LIB沒有sqlite3_prepare_v2,那麼試試這個.. sqlite3_prepare_v2可以通過sqlite3_prepare更換,但需要更多的照顧,因爲它改變了後續調用的語義略有下降。

+1

我的榮幸.... :) –

4

傢伙,在C中使用sqlite3/C++創建數據庫,這裏我使用follwing步驟.. 。

1) Firstly you include MinGw file . 
2) Add header file sqlite3.h, sqlite3.c in your src folder. 
3) Add libr folder , in libr here include these file 

    mysqlite.h, shell.c, sqlite3.c, sqlite3.h, sqlite3ext.h 

之後再啓動您的編碼...

 #include <iostream> 
using namespace std; 
#include "sqlite3.h" 

int main (int argc, const char * argv[]) { 

    sqlite3 *db; 
    sqlite3_open("test1.db", & db); 

    string createQuery = "CREATE TABLE IF NOT EXISTS items (userid INTEGER PRIMARY KEY, ipaddr 
     TEXT,username TEXT,useradd TEXT,userphone INTEGER,age INTEGER, " 
                "time TEXT NOT NULL DEFAULT 
                      (NOW()));"; 
    sqlite3_stmt *createStmt; 
    cout << "Creating Table Statement" << endl; 
    sqlite3_prepare(db, createQuery.c_str(), createQuery.size(), &createStmt, NULL); 
    cout << "Stepping Table Statement" << endl; 
    if (sqlite3_step(createStmt) != SQLITE_DONE) cout << "Didn't Create Table!" << endl; 

    string insertQuery = "INSERT INTO items (time, ipaddr,username,useradd,userphone,age) 
     VALUES ('7:30', '192.187.27.55','vivekanand','kolkatta','04456823948',74);"; // WORKS! 
    sqlite3_stmt *insertStmt; 
    cout << "Creating Insert Statement" << endl; 
    sqlite3_prepare(db, insertQuery.c_str(), insertQuery.size(), &insertStmt, NULL); 
    cout << "Stepping Insert Statement" << endl; 
    if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl; 



     return 0; 
    } 
+0

令人驚歎的答案。非常感謝好友! :-) –

+0

而不是將插入查詢的值部分保留爲靜態文本,如果在語句準備完成後更新示例,可能會更好,如何動態更改值部分。 – phoad

+0

phoad,您可以通過使用「?」或者?1?2等,然後將參數綁定到語句。參見http://www.sqlite.org/c3ref/bind_blob.html –

相關問題