- 论坛徽章:
- 0
|
Hello CU friends, I got an error in the following code, I suppose it is right, and it has compiled succeeded; but when I run it, the terminal shows an error with :
Cannot insert data into artist with error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INTERT INTO artist VALUES (8, "Shakira")' at line 1.
Please help me, thanks!
#include <mysql.h>
#include <stdio.h>
int insert_data(MYSQL *conn, char *table_name, char *sqlcmd);
int main(int argc, char *argv[])
{
MYSQL *conn = mysql_init(NULL);
if(conn == NULL)
{
printf("init mysql server error: %s\n", mysql_error(conn));
return -1;
}
if(!mysql_real_connect(conn, "localhost", "root", "db3788", "music",
0, NULL, 0))
{
printf("Connect to mysql server error: %s\n", mysql_error(conn));
return -1;
}
char sqlcmd[64];
int id = 8;
char *str = "Shakira";
char *table_name = "artist";
sprintf(sqlcmd, "INTERT INTO %s VALUES (%d, \"%s\")", table_name, id, str);
insert_data(conn, table_name, sqlcmd);
mysql_close(conn);
return 0;
}
int insert_data(MYSQL *conn, char *table_name, char *sqlcmd)
{
if(mysql_query(conn, sqlcmd))
{
printf("Cannot insert data into %s with error: %s\n",
table_name, mysql_error(conn));
return -1;
}
else
printf("inserted\n");
return 0;
}
|
|