Mysql prepared statement: Update query

I am trying to execute a C program using the mysql C API, connecting to mysql asking for an update, and I am not getting any compilation or error links, but the rows are not updating in the db table.

When I run this code, I get empty values ​​updated in emp. status field

#define STRING_SIZE 256 char* eStatus,myeStatus; int myempid,empid; int i; for(i = 0; i < 5 ; i++){ const char* sqlQuery = "update employee_info set estatus = ? where empID = ?"; if (mysql_stmt_prepare(stmt, sqlQuery, strlen(sqlQuery))) { fprintf(stderr, " mysql_stmt_prepare(), update failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); return -1; } memset(param, 0, sizeof(param)); /* zero the structures */ if (info.state == 2) eStatus = "present"; else eStatus = "absent"; empid = i; // Init param structure // Select param[0].buffer_type = MYSQL_TYPE_STRING; param[0].buffer = (void *) &eStatus; param[0].buffer_length = STRING_SIZE; param[0].is_null = 0; param[0].length = &str_length; param[1].buffer_type = MYSQL_TYPE_SHORT; param[1].buffer = (void *) &myempID; param[1].buffer_length = STRING_SIZE; param[1].is_null = 0; param[1].length = 0; myeStatus = eStatus; myempid = empid; if (mysql_stmt_bind_param(stmt, param) != 0) { fprintf(stderr, " mysql_stmt_bind_param() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); return -1; } /* Execute the statement */ if (mysql_stmt_execute(stmt)) { fprintf(stderr, " mysql_stmt_execute(), failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); return -1; } } // end of for loop 

Table schema in mysql

empid INT (11)

estatus varchar (10)

I can not understand why the status is not updated in the mysql table. Is this a data type mismatch, or are the values ​​not properly bound to sqlquery?
Any clue? Thanks.

+6
source share
2 answers

Why are you trying to use "where is empID =?" If you want it to run for every employee, just omit the where clause. If this is for a specific employee, then his identifier should be there.

There may be more problems, but this was the first I found.

You can verify by running the same query at the mysql command line.

Edit: I also do not see any database connection and no information related to this. Sort of

  MYSQL *conn = mysql_init(NULL); *conn = mysql_real_connect(*conn, DB_HOST, DB_USER, DB_PASS, DB_NAME, 0, NULL, flags); if (*conn != NULL) { printf("Connection Successfull\n"); status = 0; } 
+1
source

You can find it here: Writing to the mysql database from one on-board computer using c a complete example of how to use the MYSQL C API to execute queries, if you still have some problems, please write all the code.

+1
source

Source: https://habr.com/ru/post/924664/


All Articles