Satyam
Satyam

Reputation: 1682

C++:: How to concatenate variables using sqlite3 insertquery

Here I'm trying to concatenate variables(int and char) by insertQuery, but I'm getting error, My error is error: invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+' Guys Please Guide me where I'm wrong..

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

Upvotes: 0

Views: 1819

Answers (1)

hmjd
hmjd

Reputation: 121971

(Assuming null terminated char* or null terminated char[])

Recommend using a std::ostringstream (#include <sstream>) to construct the SQL query:

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

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

It would also be possible by creating temporary std::string objects in the concatenation sequence:

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

If a char* or char[] is not null terminated you can inform the std::string constructor how long it is:

std::string s(buf, 2);

Upvotes: 2

Related Questions