#include #include #include "../include/db_def.h" #include "../include/db_Ydef.h" #include "../include/db_udef.h" MYSQL *mySQL; char ordre[2048]; char criteria1[256]; char criteria2[256]; unsigned char mybuffer[2048]; unsigned char *pbuf; MYSQL_STMT *CurrStmtHdl; MYSQL_BIND *bind_params; MYSQL_BIND *bind_results; unsigned long *lenres; my_bool *is_null; int nbparams; int nbresults; int nb_rows; int i; int retour; int sizebind; #define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS test_table" #define CREATE_SAMPLE_TABLE "CREATE TABLE test_table(col1 CHAR(20),\ col2 char(5),\ col3 char(5))" #define INSERT_SAMPLE "INSERT INTO test_table(col1,col2,col3) VALUES('val1','1','1')" main() { int ind; mySQL = mysql_init((MYSQL*) 0); if(mySQL) { mySQL = mysql_real_connect(mySQL, "localhost", "root", "dd270450", "MFTMY", 0, NULL, 0 ); if(!mySQL ) { printf( "deb - Error on real_connect\n" ); return(-1); } } else { printf("Init mySQL Error\n"); return(-1); } printf("Connect OK\n"); if (mysql_query(mySQL, DROP_SAMPLE_TABLE)) { fprintf(stderr, " DROP TABLE failed\n"); fprintf(stderr, " %s\n", mysql_error(mySQL)); exit(0); } if (mysql_query(mySQL, CREATE_SAMPLE_TABLE)) { fprintf(stderr, " CREATE TABLE failed\n"); fprintf(stderr, " %s\n", mysql_error(mySQL)); exit(0); } if (mysql_query(mySQL, INSERT_SAMPLE)) { fprintf(stderr, " INSERT TABLE failed\n"); fprintf(stderr, " %s\n", mysql_error(mySQL)); exit(0); } printf("Do you want to test with 1 or 2 criterias ?\n"); ind = getchar(); if(ind == 0x31) sprintf(ordre,"SELECT col1 FROM test_table WHERE (col2 = ? )"); else sprintf(ordre,"SELECT col1 FROM test_table WHERE (col2 = ? and col3 = ? )"); printf("Select statement <%s>\n",ordre); printf("First SELECT---------------------------------------\n"); CurrStmtHdl = mysql_prepare( mySQL, ordre, strlen(ordre)); if( CurrStmtHdl == NULL ) { printf( "Error on prepare\n" ); return( -1 ); } printf( "prepare OK\n" ); /* Bind results */ nbresults=mysql_num_fields(mysql_get_metadata(CurrStmtHdl)); printf("nbresults = <%d>\n",nbresults); sizebind=sizeof(MYSQL_BIND) * nbresults; bind_results = (MYSQL_BIND *)malloc(sizebind); lenres=(long *)malloc(sizeof(long)*nbresults); is_null=(my_bool *)malloc(sizeof(my_bool)*nbresults); //memset(bind_results,0,sizebind); bind_results[0].buffer_type = MYSQL_TYPE_STRING; bind_results[0].buffer = (char *)mybuffer; bind_results[0].buffer_length = 20; lenres[0] = 20; bind_results[0].length = &lenres[0]; bind_results[0].is_null = &is_null[0]; if (mysql_bind_result(CurrStmtHdl, bind_results)) { printf("mysql_bind_results() failed\n"); return(-1); } printf( "bind results OK\n" ); /* make parmeters binding */ nbparams=mysql_param_count(CurrStmtHdl); printf("nbparams = <%d>\n",nbparams); /* allocate binding parameters buffer */ bind_params = (MYSQL_BIND *)malloc(sizeof(MYSQL_BIND)*nbparams); lenres=(long *)malloc(sizeof(long)*nbparams); is_null=(my_bool *)malloc(sizeof(my_bool)*nbparams); /* set criteria value */ memset(criteria1,0,256); sprintf(criteria1,"1"); /* and bind it */ bind_params[0].buffer_type = MYSQL_TYPE_STRING ; bind_params[0].buffer = (char *)criteria1; bind_params[0].buffer_length= 1; lenres[0] = 1; bind_params[0].length= &lenres[0]; is_null[0] = 0; bind_params[0].is_null= &is_null[0]; if(nbparams > 1) { memset(criteria2,0,256); sprintf(criteria2,"1"); bind_params[1].buffer_type = MYSQL_TYPE_STRING ; bind_params[1].buffer = (char *)criteria2; bind_params[1].buffer_length= 1; lenres[1] = 1; bind_params[1].length= &lenres[1]; is_null[1] = 0; bind_params[1].is_null= &is_null[1]; } if (mysql_bind_param(CurrStmtHdl, bind_params)) { printf("mysql_bind_param() failed\n"); return(-1); } printf( "bind params OK\n" ); // execute the SQL request, now retour = mysql_execute(CurrStmtHdl); if(retour != 0) { printf("execute failed <%s>\n",mysql_stmt_error(CurrStmtHdl)); return(-1); } printf("Execute OK\n"); retour = mysql_stmt_store_result(CurrStmtHdl); if(retour) { printf("stmt_store_result failed\n"); return(-1); } printf("stmt_store_result OK\n"); nb_rows=mysql_stmt_num_rows(CurrStmtHdl); printf("nb_rows = <%d>\n",nb_rows); if(nb_rows) { retour = mysql_fetch(CurrStmtHdl); if(retour) { printf("fetch failed %d <%s>\n",retour,mysql_stmt_error(CurrStmtHdl)); return(-1); } printf("fetch OK <%s>\n", mybuffer); }else{ printf("No rows to fetch : exit !\n"); return(0); } printf("Second SELECT---------------------------------------\n"); memset(criteria1,0,256); sprintf(criteria1,"1"); /* and bind it */ bind_params[0].buffer_type = MYSQL_TYPE_STRING ; bind_params[0].buffer = (char *)criteria1; bind_params[0].buffer_length= 1; lenres[0] = 1; bind_params[0].length= &lenres[0]; is_null[0] = 0; bind_params[0].is_null= &is_null[0]; if(nbparams > 1) { memset(criteria2,0,256); sprintf(criteria2,"1"); bind_params[1].buffer_type = MYSQL_TYPE_STRING ; bind_params[1].buffer = (char *)criteria2; bind_params[1].buffer_length= 1; lenres[1] = 1; bind_params[1].length= &lenres[1]; is_null[1] = 0; bind_params[1].is_null= &is_null[1]; } if (mysql_bind_param(CurrStmtHdl, bind_params)) { printf("mysql_bind_param() failed\n"); return(-1); } printf( "bind params OK\n" ); /* execute the SQL request, now */ retour = mysql_execute(CurrStmtHdl); if(retour != 0) { printf("execute failed <%s>\n",mysql_stmt_error(CurrStmtHdl)); return(-1); } printf("Execute OK\n"); retour = mysql_stmt_store_result(CurrStmtHdl); if(retour) { printf("stmt_store_result failed\n"); return(-1); } printf("stmt_store_result OK\n"); nb_rows=mysql_stmt_num_rows(CurrStmtHdl); printf("nb_rows = <%d>\n",nb_rows); if(nb_rows) { retour = mysql_fetch(CurrStmtHdl); if(retour) { printf("fetch failed %d <%s>\n",retour,mysql_stmt_error(CurrStmtHdl)); return(-1); } printf("fetch OK <%s>\n",mybuffer); }else{ printf("No rows to fetch : exit !\n"); return(0); } }