c++mysqlmysql++

how to pass C++ variables to Insert of c++ using Mysql ++


I am struggling with the code from few days can anyone help

std::string str=uri_req1.substr(found+1);
char query[2000];
sprintf(query,"Insert into publish VALUES('%s','NO')",str);

I am getting following warnings and value is not inserted in the tables

warning: cannot pass objects of non-POD type ‘struct std::string’
through ‘...’; call will abort at runtime
warning: format ‘%s’ expects type ‘char*’, but
argument 3 has type ‘int’

other thing I tried was

string query;
query="Insert into publish values('";
query+=str;
query+="','NO')";
  mysql_query(&mysql,query);

but this also never worked kindly help


Solution

  • I'd probably use a stringstream to assemble the string:

    std::ostringstream query;
    
    query << "Insert into publish values('" << str << "', 'NO')";
    mysql_query(&mysql, query.str().c_str());