我尝试执行以下操作。
添加记录后,我将从同一个表中选择以获取用户标识,即自动增量和主键。但是当我尝试执行以下操作时,我得到了分段核心转储
插入代码有效,但是当我尝试执行 select 语句时,这就是我的代码导致错误的地方。我正在尝试从数据库中获取用户 ID 字段并将其分配给字符串或向量结果 [0];
我的 test2.cpp 代码:
#include <iostream>
#include <sqlite3.h>
#include <vector>
//g++ -o test2 test2.cpp -lsqlite3
using namespace std;
string quotesql( const string& s ) {
return string("'") + s + string("'");
}
int main()
{
int counter = 0;
sqlite3 *db;
sqlite3_stmt * stmt, * stmt2;
string name = "Kungfu Panda2";
string department = "normal";
string password = "hellopassword";
string sqlstatement = "INSERT INTO abe_account (name,department,password) VALUES ("
+quotesql(name) + ","
+quotesql(department) + ","
+quotesql(password) + ");";
if (sqlite3_open("abeserver.db", &db) == SQLITE_OK)
{
sqlite3_prepare( db, sqlstatement.c_str(), -1, &stmt, NULL );//preparing the statement
sqlite3_step( stmt );//executing the statement
}
else
{
cout << "Failed to open db\n";
}
sqlite3_finalize(stmt);
//after insert ,we select back to get the user id;
sqlstatement = "select * from abe_account where name="
+quotesql(name) + " AND department="
+quotesql(department)+";";
sqlite3_prepare( db, sqlstatement.c_str(), -1, &stmt2, NULL );//preparing the statement
sqlite3_step( stmt2 );//executing the statement
std::vector< std::vector < std:: string > > result;
while( sqlite3_column_text( stmt2, 0 ) )
{
for( int i = 0; i < 4; i++ )
result[i].push_back( std::string( (char *)sqlite3_column_text( stmt2, i ) ) );
sqlite3_step( stmt2 );
}
//cout << result.size() << endl;
sqlite3_finalize(stmt2);
sqlite3_close(db);
return 0;
}
如果添加成功,如何检索我的数据。我需要检索以检查我的语句是否正确执行,并获取自动递增并创建为主键的用户 ID,因为我需要 cout 到客户端,他的用户 ID 是 +userid+
如果我对 select 语句的向量使用错误,如何在我的 insert 语句执行后获取用户 ID,我的 insert 语句在我检查我的数据库时工作,记录已添加