#include #include #include #include int main() { MYSQL* sql; MYSQL_STMT* stmt; MYSQL_BIND bind, bind_result; int id, col, rows; unsigned long id_len, col_len; char query1[] = "create database if not exists test"; char query2[] = "drop table if exists test.t;"; char query3[] = "create table test.t(id int not null, col int not null)"; char query4[] = "insert into test.t (id, col) values (1,100), (1,200),(2,300)"; char query5[] = "select col from test.t where id = ?"; char *server_options[] = {"test", "--defaults-file=my_embedded.cnf", NULL}; int num_elements = sizeof(server_options)/ sizeof(char *) - 1; printf("starting server...\n"); if(mysql_library_init(num_elements, server_options, NULL)) { printf("failed to initialize server"); return 1; } printf("connect to server...\n"); sql = mysql_init(NULL); if (!mysql_real_connect(sql, NULL , NULL , NULL , NULL, 0, NULL, 0)) printf("failed to connect\n"); if (mysql_query(sql, query1)) printf("failed to create database:%s\n", mysql_error(sql)); if (mysql_query(sql, query2)) printf("failed to use database:%s\n", mysql_error(sql)); if (mysql_query(sql, query3)) printf("failed to create table:%s\n", mysql_error(sql)); if (mysql_query(sql, query4)) printf("failed to insert record:%s\n", mysql_error(sql)); if(!mysql_stmt_init(sql)) printf("failed to init stmt:%s\n", mysql_error(sql)); if (mysql_stmt_prepare(stmt, query5, strlen(query5))) printf("failed to prepare:%s\n", mysql_stmt_error(stmt)); memset(&bind, 0 , sizeof(bind)); bind.buffer_type = MYSQL_TYPE_LONG; bind.is_unsigned = (my_bool)0; bind.is_null = 0; bind.buffer = (char*)&id; bind.buffer_length = 4; bind.length = &id_len; memset(&bind_result, 0 , sizeof(bind_result)); bind_result.buffer_type = MYSQL_TYPE_LONG; bind_result.is_unsigned = (my_bool)0; bind_result.is_null = 0; bind_result.buffer = (char*)&col; bind_result.buffer_length = 4; bind_result.length = &col_len; if (mysql_stmt_bind_param(stmt, &bind)) printf("failed to bind param:%s\n", mysql_stmt_error(stmt)); if (mysql_stmt_bind_result(stmt, &bind_result)) printf("failed to bind param:%s\n", mysql_stmt_error(stmt)); id = 1; if (mysql_stmt_execute(stmt)) printf("failed to execute stmt:%s\n", mysql_stmt_error(stmt)); if (mysql_stmt_store_result(stmt)) printf("failed to store result:%s\n", mysql_stmt_error(stmt)); rows = mysql_stmt_num_rows(stmt); printf("result: %d rows\n", rows); while (rows-- && !mysql_stmt_fetch(stmt)) printf("id = %d, col = %d, length = %d\n", id, col, col_len); mysql_stmt_free_result(stmt); id = 2; if (mysql_stmt_execute(stmt)) printf("failed to execute stmt:%s\n", mysql_stmt_error(stmt)); if (mysql_stmt_store_result(stmt)) printf("failed to store result:%s\n", mysql_stmt_error(stmt)); rows = mysql_stmt_num_rows(stmt); printf("result: %d rows\n", rows); while (rows-- && !mysql_stmt_fetch(stmt)) printf("id = %d, col = %d, length = %d\n", id, col, col_len); mysql_stmt_free_result(stmt); printf("closing stmt...\n"); mysql_stmt_close(stmt); printf("closing conneciton...\n"); mysql_close(sql); printf("shutdown server...\n"); mysql_library_end(); return 0; }