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

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

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

you just made my day!


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.