I can’t get the following code to work. It’s working with normal select or insert, but when I try to insert a variable to it it didn’t work. Can some let me know please what’s wrong here?
Please note that the output is below.
int main(int argc, char **argv)
{
MYSQL *conn;
char str[100] = "test";
conn = mysql_init(NULL);
char stmt_buf[100];
sprintf (stmt_buf, "insert into test values ('%s')", str);
printf("\n%s\n",stmt_buf);
mysql_query (conn, stmt_buf);
mysql_close(conn);
return 0;
}
~$./version
insert into test values ('test')
Segmentation fault
I didn’t see you are connected to database by
mysql_real_connectalso there is no
mysql_select_dbandmysql_exec_sqlcall.Here is an example for you.