2012-08-14 27 views
0

C++ sqlite3如何知道sql語句執行成功?C++ sqlite3如何知道sql語句執行成功?

我試圖插入一條記錄與sqlite3的

我想知道如何知道它的成功

string sqlstatement = "INSERT INTO abe_account (name,department,password) VALUES (" 
+quotesql(name) + "," 
+quotesql(department) + "," 
+quotesql(password) + ");"; 

    if (sqlite3_open("abeserver.db", &db) == SQLITE_OK) 
    { 
    sqlite3_prepare(db, sqlstatement.c_str(), -1, &stmt, NULL);//preparing the statement 
    sqlite3_step(stmt);//executing the statement 
     } 
    else 
    { 
     cout << "Failed to open db\n"; 
    } 

    sqlite3_finalize(stmt); 

我們怎樣從sqlite3的成功信號時,一行被成功添加狀或一行被成功刪除等,並賦值爲一個布爾值或一個字符串

回答

1

sqlite3_step()有一個整數形式的返回值的數量。代碼0表示一個語句的成功執行。你可以找到的代碼here

其餘我們如何..該值賦值給一個變量

if (! sqlite3_step(stmt)) cout << "it worked" << endl 
+0

Python? :)'if(!sqlite3_step(stmt))cout <<「it works」<< endl;' – 2012-08-14 15:31:10

+0

@ Component10對不起,誤解了一秒鐘。謝謝 – 2012-08-14 15:54:32

1

你也可以做

if(sqlite3_step(stmt)!=SQLITE_DONE) 
{ 
    error handling here 
} 

而且這裏的一些文檔on sqlite3常量:Result Codes