Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQLite C++程序值仅在第二次不被插入时才被插入_C++_Sqlite - Fatal编程技术网

SQLite C++程序值仅在第二次不被插入时才被插入

SQLite C++程序值仅在第二次不被插入时才被插入,c++,sqlite,C++,Sqlite,当我尝试插入值时,第一次一切正常,但第二次不接受值,我应该怎么做。? 创建表后,我尝试插入值,但效果很好,现在再次尝试插入值,但不接受值 另外,当我想选择时,它只显示第1列,即用户id,但我想显示整个列 #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #includ

当我尝试插入值时,第一次一切正常,但第二次不接受值,我应该怎么做。? 创建表后,我尝试插入值,但效果很好,现在再次尝试插入值,但不接受值 另外,当我想选择时,它只显示第1列,即用户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;
}

因为每次插入后都会关闭数据库连接

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;
在程序开始时打开数据库连接一次,因此应在程序结束时关闭数据库连接一次


为了回应埃米尔所说的,看在上帝的份上,正确地缩进你的代码!如果您的代码不是那么混乱,您自己可能已经发现了这一点。

运行代码时会发生什么?会发生什么?到目前为止,您做了哪些故障排除?请缩进你所有的代码!如果你想获得好成绩和/或避免被解雇,你应该研究SQL注入。我删除了sqlite3_closedb语句,但问题仍然存在。好的,但你应该在程序结束时关闭数据库连接。因此,仅仅删除它不是正确的做法。我做了一件什么都没有发生的事,然后您应该检查从sqlite3_步骤和sqlite3_重置返回的错误代码,以提供出错的线索。