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

9 Years
Discussion Span
Last Post by lifted

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.