mysql prepared statements and MYSQL_ROW

Viewed 47

Is it possible to get MYSQL_ROW when using mysql prepared statements? We are replacing legacy code by mysql prepared statements in C. However, due to the way mysql prepared statements returns result, the task has become huge.

The old code was using MYSQL_RES and MYSQL_ROW

MYSQL_ROW row = mysql_fetch_row(res);

while prepared statements requires us to bind each variable

mysql_stmt_bind_result(stmt, bind);
mysql_stmt_fetch(stmt);

I was unable to find any stmt API that returns ROW which is surprising. Have I overlooked something obvious?

If not, I plan to explore MYSQL_STMT structure which has some resemble to MYSQL_RES, especially the data_cursor variable.

typedef struct MYSQL_STMT {
  struct MEM_ROOT *mem_root; /* root allocations */
  LIST list;                 /* list to keep track of all stmts */
  MYSQL *mysql;              /* connection handle */
  MYSQL_BIND *params;        /* input parameters */
  MYSQL_BIND *bind;          /* output parameters */
  MYSQL_FIELD *fields;       /* result set metadata */
  MYSQL_DATA result;         /* cached result set */
  MYSQL_ROWS *data_cursor;   /* current row in cached result */

Any suggestions?

Thank a ton for your help.

1 Answers

You, you haven't missed anything. Using the C API requires a lot of meticulous code to set up buffers for each column of a row. This is because it's the client's responsibility to allocate memory for each column.

A full example is shown in the 'Example' section of this page: https://dev.mysql.com/doc/c-api/8.0/en/mysql-stmt-fetch.html

Some other languages create wrapper libraries to do this work in a generalized manner. They call mysql_stmt_result_metadata() before fetching rows, and allocate buffers for each column based on the column types. Then once a row is fetched, create a MYSQL_ROW or some other struct that suits your application.

Related