c - How do I insert data into mysql table? -
i trying insert data mysql database. connection , displaying of data works.
but don't know how use insert table
command in c code.
i've tried reading strings scanf
/getchar
values in mysql command, didn't work.
how insert data mysql table after read data in program?
i'm working in linux.
this source code:
#include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h> static char *host = "localhost"; static char *user = "root"; static char *pass = "password"; static char *dbname = "tutorial"; unsigned int port = 3306; static char *unix_socket = null; unsigned int flag = 0; int main() { mysql *conn; mysql_res * res; mysql_row row; conn = mysql_init(null); if(!(mysql_real_connect(conn, host, user, pass, dbname, port, unix_socket, flag))) { fprintf(stderr, "error: %s[%d]", mysql_error(conn), mysql_errno(conn)); exit(1); } mysql_query(conn, "select * users"); res = mysql_store_result(conn); while(row = mysql_fetch_row(res)) { printf("%s\t%s\n", row[0], row[1]); } mysql_free_result(res); mysql_close(conn); return exit_success; }
i tried:
... int id[1] = 5; char name[8] = "jack"; ... mysql_query(conn, insert users(id, name) values(id, name); ...
you must prepare c-string before query creation.
you can use snprintf so:
#define max_string 128 char query[max_string] = {0}; int id = 5; char name[] = "jack"; snprintf(query, max_string, "insert users (id, name) values (%d, '%s')", id, name); mysql_query(conn, query);
as @viraptor pointed out the solution above has problems sql injection. should use mysql api (coded totally safe) job:
#define insert_query = "insert users (id, name) values (?,?)"; int id = 5; char name[] = "jack"; size_t str_length = strlen(name); mysql_stmt *stmt = mysql_stmt_init(mysql); if (!stmt) { fprintf(stderr, " mysql_stmt_init(), out of memory\n"); exit(0); } if (mysql_stmt_prepare(stmt, insert_query, strlen(insert_query))) { fprintf(stderr, " mysql_stmt_prepare(), insert failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } mysql_bind bind[2]; /* integer param */ /* number type, there no need specify buffer_length */ bind[0].buffer_type= mysql_type_long; bind[0].buffer= (char *)&id; bind[0].is_null= 0; bind[0].length= 0; /* string param */ bind[1].buffer_type= mysql_type_string; bind[1].buffer= (char *)name; bind[1].buffer_length= str_length+1; bind[1].is_null= 0; bind[1].length= &str_length; /* bind buffers */ if (mysql_stmt_bind_param(stmt, bind)) { fprintf(stderr, " mysql_stmt_bind_param() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* execute insert statement - 2*/ if (mysql_stmt_execute(stmt)) { fprintf(stderr, " mysql_stmt_execute, 2 failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* total rows affected */ my_ulonglong affected_rows= mysql_stmt_affected_rows(stmt); fprintf(stdout, " total affected rows(insert 2): %lu\n", (unsigned long) affected_rows); if (affected_rows != 1) /* validate affected rows */ { fprintf(stderr, " invalid affected rows mysql\n"); exit(0); } /* close statement */ if (mysql_stmt_close(stmt)) { fprintf(stderr, " failed while closing statement\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); }
you can find reference code at link
Comments
Post a Comment