当我尝试插入值时,一切正常,但第二次它没有取值,我该怎么做。 创建表后我尝试插入工作正常的值,现在再次第二次,如果我尝试插入它不采取值 当我想选择它时,它只显示第一列是用户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;
}
答案 0 :(得分: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所说的,为了上帝的缘故,正确缩进你的代码!如果您的代码不是那么混乱,您可能已经发现了这一点。