#include "my_global.h" #include "mysql.h" #define SELECT123 "SELECT TABNAME ,VARKEY ,DATALN ,VARDATA FROM PVER1 WHERE TABNAME = ? ORDER BY TABNAME ,VARKEY" int main(int argc, char **argv) { MYSQL *conn; MYSQL_STMT *prep_stmt = NULL, *prep_stmt2; MYSQL_BIND bind, *bind_out, *bind_out2; const char *row_data; char *out_row_data[6000]={0}, *out_row_data2[6000]; int res, i; const ulong type= (ulong)CURSOR_TYPE_READ_ONLY; unsigned long blen=6; mysql_library_init(argc, argv, NULL); conn = mysql_init(NULL); conn = mysql_real_connect(conn, "10.17.69.84", "root", "mysql50", "MY2", 0, NULL, 0); if (!conn) exit(1); mysql_query(conn, "set autocommit=0"); prep_stmt= mysql_stmt_init(conn); if (!prep_stmt) { fprintf(stderr, "Out of memory when creating the statement"); exit(2); } prep_stmt2= mysql_stmt_init(conn); if (!prep_stmt2) { fprintf(stderr, "Out of memory when creating the statement"); exit(2); } printf("prep_stmt init done\n"); if ((res= mysql_stmt_prepare(prep_stmt, SELECT123 , strlen(SELECT123)))) { fprintf(stderr, "error while preparing %d", res); exit(3); } mysql_stmt_attr_set(prep_stmt, STMT_ATTR_CURSOR_TYPE, (const void *)&type); printf("cursor set\n"); memset(&bind, 0, sizeof(MYSQL_BIND)); bind.buffer_type= MYSQL_TYPE_STRING; bind.buffer= "TAPOPA"; bind.buffer_length= 6; bind.length= NULL; if ((res= mysql_stmt_bind_param(prep_stmt, &bind))) { fprintf(stderr, "Error while executing the query %s::%d %d", __FILE__, __LINE__, res); exit(31); } if ((res= mysql_stmt_execute(prep_stmt))) { fprintf(stderr, "Error while executing the query %d", res); exit(4); } printf("cursor executed\n"); bind_out= (MYSQL_BIND *) malloc(4 * sizeof(MYSQL_BIND)); memset(bind_out, 0, 4 * sizeof(MYSQL_BIND)); bind_out[0].buffer_type= MYSQL_TYPE_STRING; bind_out[0].buffer= &out_row_data[0]; bind_out[0].buffer_length= 10; bind_out[0].length= NULL; bind_out[1].buffer_type= MYSQL_TYPE_STRING; bind_out[1].buffer= &out_row_data[10]; bind_out[1].buffer_length= 110; bind_out[1].length= NULL; bind_out[2].buffer_type= MYSQL_TYPE_SHORT; bind_out[2].buffer= &out_row_data[120]; bind_out[2].buffer_length= 0; bind_out[2].length= NULL; bind_out[3].buffer_type= MYSQL_TYPE_BLOB; bind_out[3].buffer= &out_row_data[124]; bind_out[3].buffer_length= 3800; bind_out[3].length= NULL; if ((res= mysql_stmt_bind_result(prep_stmt, bind_out))) { fprintf(stderr, "Error while binding the result %d", res); exit(11); } int j=0; for (j=0; j < 22; j++) { res= mysql_stmt_fetch(prep_stmt); if (res == MYSQL_DATA_TRUNCATED) { printf("Data truncation!\n"); } for (i= 0; i < 1; ++i) { if (bind_out[i].error) { printf("TRUNC[%d]!", i); } printf("%*s\n", 110, &out_row_data[10]); printf("%d=%s ",i, (char *)(bind_out[i].buffer)); } printf("*"); } printf("\n"); if ((res= mysql_stmt_prepare(prep_stmt2, SELECT123 , strlen(SELECT123)))) { fprintf(stderr, "error while preparing %d", res); exit(3); } mysql_stmt_attr_set(prep_stmt2, STMT_ATTR_CURSOR_TYPE, (const void *)&type); printf("cursor2 set\n"); memset(&bind, 0, sizeof(MYSQL_BIND)); bind.buffer_type= MYSQL_TYPE_STRING; bind.buffer= "TAPOPA"; bind.buffer_length= 6; bind.length= NULL; if ((res= mysql_stmt_bind_param(prep_stmt2, &bind))) { fprintf(stderr, "Error while executing the query %s::%d %d", __FILE__, __LINE__, res); exit(31); } if ((res= mysql_stmt_execute(prep_stmt2))) { fprintf(stderr, "Error while executing the query %d", res); exit(4); } printf("cursor2 executed\n"); bind_out2= (MYSQL_BIND *) malloc(4 * sizeof(MYSQL_BIND)); memset(bind_out2, 0, 4 * sizeof(MYSQL_BIND)); bind_out2[0].buffer_type= MYSQL_TYPE_STRING; bind_out2[0].buffer= &out_row_data2[0]; bind_out2[0].buffer_length= 10; bind_out2[0].length= NULL; bind_out2[1].buffer_type= MYSQL_TYPE_STRING; bind_out2[1].buffer= &out_row_data2[10]; bind_out2[1].buffer_length= 110; bind_out2[1].length= NULL; bind_out2[2].buffer_type= MYSQL_TYPE_SHORT; bind_out2[2].buffer= &out_row_data2[120]; bind_out2[2].buffer_length= 0; bind_out2[2].length= NULL; bind_out2[3].buffer_type= MYSQL_TYPE_BLOB; bind_out2[3].buffer= &out_row_data2[124]; bind_out2[3].buffer_length= 3800; bind_out2[3].length= NULL; if ((res= mysql_stmt_bind_result(prep_stmt2, bind_out2))) { fprintf(stderr, "Error while binding the result %d", res); exit(11); } for (j=0; j < 18; j++) { res= mysql_stmt_fetch(prep_stmt2); if (res == MYSQL_DATA_TRUNCATED) { printf("Data truncation!\n"); } for (i= 0; i < 1; ++i) { if (bind_out2[i].error) { printf("TRUNC[%d]!", i); } printf("%*s\n", 110, &out_row_data2[10]); printf("%d=%s ",i, (char *)(bind_out2[i].buffer)); } printf("*"); } printf("\n"); if (mysql_stmt_close(prep_stmt)) { fprintf(stderr, "Error2 while binding the result %d", res); exit(12); } if (mysql_query(conn, "commit")) { fprintf(stderr, "Error2 while binding the result %d", res); exit(12); } if (res= mysql_stmt_fetch(prep_stmt2)) { fprintf(stderr, "Error3 while fetching the result %d", res); exit(12); } }