954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

create mysql query

Hello,

i'm very new to c++ and i'm trying to create some sql but dont really know how.

i need a very simple query in php i would do something like:

$page = "pagedata";
$sql = "INSERT INTO data SET field = '$page'";

i know i cant do it the same so i came up with the following:

string page;
char *sql;
while(!cin.eof()) page += cin.get();
sprintf(sql, "INSERT INTO data SET field = '%s';",page);
cout << sql;
mysql_query(connection, sql);

But that didnt work
so i tried:

string page,sql;
while(!cin.eof()) page += cin.get();
sql = "INSERT INTO data SET field = '" + page + "'";
cout << sql;
mysql_query(connection, sql);



This creates the query but gives a compile error:
cannot convert 'std::string' to 'const char*' for argument '2' to 'int mysql_query(MYSQL*, const char*)'

can anyone help me how to do this properly?
thanks in advance

lifted
Newbie Poster
4 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Try calling:
mysql_query(connection, sql.c_str());

sql.c_str() will provide the conversion to const char*.

MrSpigot
Junior Poster
158 posts since Mar 2009
Reputation Points: 76
Solved Threads: 40
 

you just made my day!

thanks

lifted
Newbie Poster
4 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You