2012-11-20 77 views
-3

當我嘗試插入值時,第一次一切正常,但第二次它不接受值,我該怎麼辦。 創建表後我嘗試插入值工作正常,現在再次如果我嘗試插入它不取值 也當我想選擇它只顯示第一列是用戶ID,但我想整個要顯示的列sqlite C++程序值只插入第一次第二次它們不插入

 #include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>  
#include <iostream> 
#include <sstream> 
#include <string> 
using namespace std; 
#include "sqlite3.h" 
int main (int argc, const char * argv[]) { 
int ch; 
int userid; 
string name; 
string sName; 
int rc; 
sqlite3 *db; 
sqlite3_open("custom1.db", & db); 
string createQuery = "CREATE TABLE IF NOT EXISTS items3 (uid INTEGER primary key ,name TEXT);"; 
std::stringstream insertQuery; 
std::stringstream selectQuery; 
std::stringstream removeQuery; 
while(ch!=5){ 
cout<<endl<<"1:create table"<<endl<<"2:insert data"<<endl<<"3:select data"<<endl<<"4:remove"<<endl<<"5:exit"<<endl; 
cout<<"enter choice"<<endl; 

cin>>ch; 
switch(ch) { 
case 1 : 
    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; 
break; 


case 2 : 
    sqlite3_stmt *insertStmt; 

    cout << "Creating Insert Statement" << endl; 
cout<<"userid:";cin>>userid;cout<<"name:";cin>>name; 
insertQuery << "INSERT INTO items3 (uid,name)" 
       " VALUES (" << userid << ", '" << name<<"')"; 
    sqlite3_prepare(db, insertQuery.str().c_str(), insertQuery.str().size(), &insertStmt, NULL); 
    cout << "Stepping Insert Statement" << endl; 
if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl; 
sqlite3_reset(insertStmt); 
sqlite3_close(db); 
break; 
case 3: 
    sqlite3_stmt *selectStmt; 

    cout << "Creating select Statement" << endl; 
cout<<"userid:"; 
cin>>userid; 
selectQuery<<"select * from items3 where uid="<<userid; 
    rc= sqlite3_prepare(db, selectQuery.str().c_str(), selectQuery.str().size(), &selectStmt, NULL); 
    cout << "Stepping select Statement" << endl; 
while (sqlite3_step(selectStmt) == SQLITE_ROW) { 
    sName = (char*)sqlite3_column_text(selectStmt, 0); 
    // Obj.Display(sName); //<== this is not display 
    cout << "userid" << sName << endl; 

    } 
sqlite3_step(selectStmt); 
    if (sqlite3_step(selectStmt) != SQLITE_DONE) cout << "Didn't Select Item!" << endl; 
     else 
    cout << "Success!" << endl; 
sqlite3_close(db); 
break; 
case 4 :sqlite3_stmt *removeStmt; 
cout<<"creating remove statement"<<endl; 
cout<<"userid:"; 
cin>>userid; 
removeQuery<<"delete from items3 where uid="<<userid; 
sqlite3_prepare(db,removeQuery.str().c_str(),removeQuery.str().size(),&removeStmt,NULL); 
cout<<"stepping remove statement"<<endl; 
if(sqlite3_step(removeStmt)!=SQLITE_DONE) 
cout<<"didn't remove item!"<<endl; 
else 
cout<<"success"<<endl; 
sqlite3_close(db); 
break; 
} 
} 
return 0; 
} 
+1

運行代碼時會發生什麼?應該發生什麼?你到目前爲止做了哪些故障排除?並請縮進你的所有代碼! –

+1

如果你想獲得好的分數和/或避免被解僱,你應該研究* SQL注入*。 – john

回答

1

因爲您在每次插入後關閉數據庫連接。

int main (int argc, const char * argv[]) 
{ 
    ... 
    sqlite3 *db; 
    sqlite3_open("custom1.db", & db); 
    ... 
    while(ch!=5) 
    { 
     ... 
     switch(ch) { 
     ... 
     case 2: 
      sqlite3_stmt *insertStmt; 
      ... 
      if (sqlite3_step(insertStmt) != SQLITE_DONE) 
       cout << "Didn't Insert Item!" << endl; 
      sqlite3_reset(insertStmt); 
      sqlite3_close(db); // <-- ERROR HERE 
      break; 

您在程序啓動時打開一次數據庫連接,因此您應該在程序結束時關閉一次數據庫連接。

爲了迴應Emil所說的話,爲了上帝的緣故,請妥善地縮進代碼!如果你的代碼不是那麼混亂,你可能自己發現了這一點。

+0

我刪除了該語句 sqlite3_close(db)但仍存在問題。 –

+0

好的,但是您應該在程序結束時關閉數據庫連接。所以只是刪除它不是正確的事情。 – john

+0

我沒有發生任何事情 –