Hey all,
sorry to bother you but I have been wrecking my brain with this issue.
I have been programming php for several years and needed some portable console apps so my natural choice was to go to c++.
Now I got stuck when trying to create an sql query for mysql that was composed of text and other variables.

int seconds = time (NULL);
string characters; 

characters = "text from user input";

char *sqlquery = "INSERT INTO chatlog (datetime, message) VALUES ('seconds','characters')";

And I get som invalid conversion errors.

In php i would simply

$sqlquery = "INSERT INTO chatlog(date,messave) VALUES ('$seconds','$characters');

How would you go about creating this SQL query.

Any suggestions? Hints?

Thanks in advance.


i don't know if it will help but you could try this:

int seconds=time (NULL);
    string characters;   	
    char seconds1[128];
    string seconds2=seconds1;
    string sqlquery="INSERT INTO chatlog (datetime, message) VALUES (" + seconds2 + "," + characters + ")";

the easiest was is to use streamstring class

#include <sstream>
stringstream sqlquery;
int seconds = time (NULL);
string characters; 
characters = "text from user input";

sqlquery << "INSERT INTO chatlog (datetime, message) VALUES ('"
          << seconds << "'','" << characters << "')";

Thanks for all the help guys, I ended up using

sprintf(sqlquery, "INSERT INTO chatlog (datetime, message) VALUES ('%i','%s')",seconds ,message);

and now it works.

Thanks again.


This article has been dead for over six months. Start a new discussion instead.