Link to home
Create AccountLog in
Avatar of cassiej81
cassiej81

asked on

How to use MYSQL Prepare Statement in C?

Hi, i need to do something with mySQL database from my program written in C.I want to use prepare statement to interact with the db?Can someone here can give me an example of using it in C?

Thanks in advance!
Avatar of Infinity08
Infinity08
Flag of Belgium image

Take a look at the MySQL documentation, specifically the chapter about the C API :

        http://dev.mysql.com/doc/refman/5.0/en/c.html


Below is an example (no error checking added !!!). A more elaborate example can be found here :

        http://dev.mysql.com/doc/refman/5.0/en/mysql-stmt-execute.html
MYSQL_STMT *stmt = mysql_stmt_init(mysql);
 
char *sqlReq = "some SQL query";
mysql_stmt_prepare(stmt, sqlReq, strlen(sqlReq));
 
/* optionally call mysql_stmt_bind_param to bind parameters */
 
mysql_stmt_execute(stmt);

Open in new window

Avatar of cassiej81
cassiej81

ASKER

Hi, thanks but exactly what i need is how to using "mysql_stmt_bind_param to bind parameters"? i not found any doc explain about it, can you give me a example about it too?
here is my code:

MYSQL_STMT *stmt = mysql_stmt_init(conn);
         char query = "INSERT INTO im(userid,nickname,type) values(?,?,?)";

         int *userid = '17';
         char *nickname = 'pisoga';
         char *type = 'yahoo';
         mysql_stmt_prepare(stmt, query, strlen(query));
         printf("prepare done\n");
            bind[0].buffer_type= MYSQL_TYPE_LONG;
            bind[0].buffer= (char *)&userid;
            bind[0].is_null= 0;
            bind[0].length= 0;

            /* STRING PARAM */
            bind[1].buffer_type= MYSQL_TYPE_STRING;
            bind[1].buffer= (char *)&nickname;
            bind[1].buffer_length= STRING_SIZE;
            bind[1].is_null= 0;
            bind[1].length= &str_length;

            /* SMALLINT PARAM */
            bind[2].buffer_type= MYSQL_TYPE_STRING;
            bind[2].buffer= (char *)&type;
            bind[2].is_null= &is_null;
            bind[2].length= 0;
            //mysql_stmt_prepare(stmt, query, strlen(query));

            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);
            }
            printf("Bind param done\n");


       if (mysql_stmt_execute(stmt)) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(0);
       }

it's return a segmentation fault error.Help, please!
for the init, which i place above the code  above :D


       MYSQL *conn;
       MYSQL_RES *res;
       MYSQL_ROW row;

       MYSQL_BIND    bind[3];
       my_ulonglong  affected_rows;
       int           param_count;
       short         small_data;
       int           int_data;
       char          str_data[STRING_SIZE];
       unsigned long str_length;
       my_bool       is_null;

       char *server = "localhost";
       char *user = "root";
       char *password = "";
       char *database = "community";
       conn = mysql_init(NULL);


       if (!mysql_real_connect(conn, server,
             user, password, database, 0, NULL, 0)) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(0);
       }
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks, my code work perfectly now.Point for you :-)