當我嘗試插入值時,第一次一切正常,但第二次它不接受值,我該怎麼辦。 創建表後我嘗試插入值工作正常,現在再次如果我嘗試插入它不取值 也當我想選擇它只顯示第一列是用戶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;
}
運行代碼時會發生什麼?應該發生什麼?你到目前爲止做了哪些故障排除?並請縮進你的所有代碼! –
如果你想獲得好的分數和/或避免被解僱,你應該研究* SQL注入*。 – john