我正在处理用户可以将记录插入到SQLite数据库的项目。 查询会在下面的方法自动生成:C++和SQLite - 如何执行由用户输入形成的查询?
string ID = "";
string title = "";
string password = "";
cout << "Insert ID:\n";
cin >> ID;
cout << "Insert title of password:\n";
cin >> title;
cout << "Insert password:\n";
cin >> password;
string sql = "INSERT INTO test (ID,title,password) VALUES(" + ID + "," + title + "," + password + ");";
当我尝试编译程序出现错误:
classes.h:74:76: error: invalid operands of types ‘const char*’ and ‘const char [2]’ to binary ‘operator+’
string sql = "INSERT INTO passwords (ID,title,password) VALUES (" + id + "," + title + "," + password
^
classes.h:78:42: error: invalid operands of types ‘int’ and ‘sqlite3_stmt*’ to binary ‘operator&’
sqlite3_prepare(db, sql.c_str(), -1 &st, NULL);
看来,他不能接受多个字符。 有人可以告诉我如何解决这个错误?
P.S.我是新的C++
任何帮助表示赞赏。谢谢。
编辑:
全码
sqlite3 *db;
sqlite3_stmt * st;
int id = 0;
string title = "";
string password = "";
cout << "Insert ID:\n";
cin >> id;
cout << "Insert title of password:\n";
cin >> title;
cout << "Insert password:\n";
cin >> password;
string sql = "INSERT INTO passwords (ID,title,password) VALUES (" + id + ',' + title + ',' + password + ");";
if(sqlite3_open("pw.db", &db) == SQLITE_OK)
{
sqlite3_prepare(db, sql.c_str(), -1 &st, NULL);
sqlite3_step(st);
}
else
{
cout << "Failed to connect\n";
}
sqlite3_finalize(st);
sqlite3_close(db);
永远不会使用原始用户输入来形成sql查询,除非您的目的是为了说明为什么它是一个坏主意。我不在乎它是否是一些玩具项目。 – Yakk
是的,这是一个供个人使用的项目。 – CMz
'std :: to_string(id)'(假设这篇文章中最后一段代码是* actual *问题,其中'id'确实被声明为'int')。 – WhozCraig