exampleを参考にしつつ。
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <libdrizzle/drizzle_client.h> static void result_info(drizzle_result_st *result) { printf("Result: row_count=%" PRId64 "\n" " insert_id=%" PRId64 "\n" " warning_count=%u\n" " column_count=%u\n\n", drizzle_result_row_count(result), drizzle_result_insert_id(result), drizzle_result_warning_count(result), drizzle_result_column_count(result)); } int main() { drizzle_st drizzle; drizzle_con_st con; drizzle_result_st result; drizzle_return_t ret; if (NULL == drizzle_create(&drizzle)) { fprintf(stderr, "drizzle_create failed\n"); exit(1); } drizzle_add_options(&drizzle, DRIZZLE_NON_BLOCKING); if (NULL == drizzle_con_add_tcp(&drizzle, &con, "192.168.200.150", 3306, "root", NULL, "mysql", DRIZZLE_CON_MYSQL)) { fprintf(stderr, "drizzle_con_add_tcp:%s\n", drizzle_error(&drizzle)); exit(1); } while (1) { drizzle_query(&con, &result, "show tables", 11, &ret); if (DRIZZLE_RETURN_OK == ret) { break; } if (DRIZZLE_RETURN_IO_WAIT != ret) { fprintf(stderr, "drizzle_query:%s\n", drizzle_error(&drizzle)); exit(1); } if (DRIZZLE_RETURN_OK != drizzle_con_wait(&drizzle)) { fprintf(stderr, "drizzle_con_wait:%s\n", drizzle_error(&drizzle)); exit(1); } while (NULL == drizzle_con_ready(&drizzle)) { fprintf(stderr, "drizzle_con_ready:%s\n", drizzle_error(&drizzle)); exit(1); } printf("."); } puts(""); result_info(&result); drizzle_result_free(&result); drizzle_con_free(&con); drizzle_free(&drizzle); return 0; }
生成系の関数でポインタの代わりにNULLを渡すとlibdrizzle側でアロケートしてくれるけど(xxx_freeで解放)、ノンブロッキングでdrizzle_queryが何度も呼ばれることを考えると、drizzle_result_stは呼び出し側でアロケートした方が良いのかも。