-3

当我尝试插入值时,第一次一切正常,但第二次没有值,我该怎么办。?创建表后,我尝试插入工作正常的值,现在第二次如果我尝试插入它也不接受值,当我想选择它时它只显示第一列是用户 ID,但我希望整个列被显示

        #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;
}
4

1 回答 1

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 所说的话,看在上帝的份上,正确缩进你的代码!如果您的代码不是那么混乱,您可能已经发现了这一点。

于 2012-11-20T07:06:26.103 回答