#include #include "mysql.h" #define SELECT123 "select id, min(name) from bug11904b group by id" #define MHOST "localhost" #define MUSER "root" #define MPASS "" #define MDBASE "test" void setup_db(MYSQL *conn) { int res; res= mysql_query(conn, "DROP TABLE IF EXISTS bug11904b"); res= mysql_query(conn, "create table bug11904b (id int, name char(10), primary key(id, name)) engine=innodb"); res= mysql_query(conn, "insert into bug11904b values (1, 'sofia'), (1,'plovdiv'), (1,'varna'), (2,'LA'), (2,'new york'), (3,'heidelberg'), (3,'berlin'), (3, 'frankfurt')"); } #define CHECK_EQ(a, b) if (a!=b) {fprintf(stderr, "expected %d but got %d at [%s:%d]\n", b, a, __FILE__, __LINE__);exit(100);} else printf("OK!\n"); #define CHECK_EQ_S(a, b) if (memcmp(a, b, strlen(b))) {fprintf(stderr, "expected %s but got %s at [%s:%d]\n", b, a, __FILE__, __LINE__);exit(100);} else printf("OK!\n"); int main(int argc, char **argv) { MYSQL *conn; MYSQL_STMT *prep_stmt = NULL; int res; MYSQL_BIND *bind; char *row_data; const ulong type= (ulong)CURSOR_TYPE_READ_ONLY; mysql_library_init(argc, argv, NULL); if (!(conn = mysql_real_connect(mysql_init(NULL), MHOST, MUSER, MPASS, MDBASE, 0, NULL, 0))) { fprintf(stderr, "Could not connect"); exit(1); } setup_db(conn); prep_stmt= mysql_stmt_init(conn); res= mysql_stmt_attr_set(prep_stmt, STMT_ATTR_CURSOR_TYPE, (const void *)&type); CHECK_EQ(res, 0); res= mysql_stmt_prepare(prep_stmt, SELECT123 , strlen(SELECT123)); CHECK_EQ(res, 0); res= mysql_stmt_execute(prep_stmt); CHECK_EQ(res, 0); bind= (MYSQL_BIND *) malloc(2 * sizeof(MYSQL_BIND)); memset(bind, 0, 2 * sizeof(MYSQL_BIND)); row_data= (char *) calloc(0, sizeof(int) + 10 + 1);// int + 10 bytes for string bind[0].buffer_type= MYSQL_TYPE_LONG; bind[0].buffer=& row_data[0]; bind[0].buffer_length= 0; bind[0].length= 0; bind[1].buffer_type= MYSQL_TYPE_STRING; bind[1].buffer=& row_data[sizeof(int)]; bind[1].buffer_length= 10; bind[1].length= 0; mysql_stmt_bind_result(prep_stmt, bind); res= mysql_stmt_fetch(prep_stmt); CHECK_EQ(res, 0); CHECK_EQ(row_data[0], 1); CHECK_EQ_S(row_data + sizeof(int), "plovdiv"); res= mysql_stmt_fetch(prep_stmt); CHECK_EQ(res, 0); CHECK_EQ(row_data[0], 2); CHECK_EQ_S(row_data + sizeof(int), "LA"); res= mysql_stmt_fetch(prep_stmt); CHECK_EQ(res, 0); CHECK_EQ(row_data[0], 3); CHECK_EQ_S(row_data + sizeof(int), "berlin"); free(bind); free(row_data); res= mysql_stmt_close(prep_stmt); CHECK_EQ(res, 0); }