C ++ ::如何使用sqlite3 insertquery连接变量

时间:2012-07-04 07:59:30

标签: c++ sqlite

这里我试图通过insertQuery连接变量(int和char),      但我得到错误,我的错误是错误:类型'const char *'和。的无效操作数     'const char [2]'到二元'运算符+'      伙计们请指导我,我错了..

     **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;**

1 个答案:

答案 0 :(得分:2)

(假设null终止char*或null终止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

如果char*char[]未终止,您可以通知std::string构造函数有多长时间:

std::string s(buf, 2);