0

在这里,我试图通过 insertQuery 连接变量(int 和 char),但出现错误,我的错误是错误:类型为 'const char*' 和 'const char [2]' 的无效操作数到二进制 'operator+'伙计们请指导我哪里错了..

     **string insertQuery= "insert into items   (user_id,session_id,user_sessionname,buffer)values("
                        + user + "," + session + ",'" + usrname +   "','" + buff + "')";

         sqlite3_stmt *insertStmt;
                   cout << "Creating Insert Statement" << endl;
                   sqlite3_prepare(db, insertQuery.c_str(), insertQuery.size(), &insertStmt, NULL);
                   cout << "Stepping Insert Statement" << endl;
                   if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl;**
4

1 回答 1

2

(假设空终止char*或空终止char[]

推荐使用std::ostringstream( #include <sstream>) 来构造 SQL 查询:

std::ostringstream s;
s << "insert into items "
  << user
  << ','
  << session;

const std::string insertQuery(s.str());

也可以通过std::string在串联序列中创建临时对象:

const std::string insertQuery= 
    std::string("insert into items ... values(")
  + std::string(user)
  + std::string(","); // etc

如果 a char*orchar[]不是 null 终止,您可以通知std::string构造函数它是多长时间:

std::string s(buf, 2);
于 2012-07-04T08:05:56.780 回答