#include #include #include #include int main() { MYSQL* sql; MYSQL_STMT* stmt; MYSQL_BIND bind; unsigned* indice; int ret; int result; unsigned long length; char query1[] = "create database if not exists mydb"; char query2[] = "use mydb"; char query3[] = "create table if not exists mytable (id int not null, col int not null)"; char query4[] = "insert into mytable (id, col) values (1,100)"; char query5[] = "select col from mytable where id = ?"; char *server_options[] = { "","--defaults-file=my.cnf","--basedir=./"}; int num_elements = sizeof(server_options)/ sizeof(char *); if(mysql_server_init(num_elements, server_options, NULL)) { puts("failed to initialize server"); return 1; } sql = mysql_init(NULL); if(!mysql_real_connect(sql, NULL , NULL , NULL , NULL, 3306, NULL, 0)) printf("failed to connect\n"); if(mysql_query(sql, query1)) printf("failed to create database:%s\n", mysql_error(sql)); ret = mysql_query(sql, query2); if(ret) printf("failed to use database:%s\n", mysql_error(sql)); ret = mysql_query(sql, query3); if(ret) printf("failed to create table:%s\n", mysql_error(sql)); ret = mysql_query(sql, query4); if(ret) printf("failed to insert record:%s\n", mysql_error(sql)); stmt = mysql_stmt_init(sql); if(!stmt) printf("failed to init stmt:%s\n", mysql_error(sql)); ret = mysql_stmt_prepare(stmt, query5, strlen(query5)); if(ret) printf("failed to prepare:%s\n", mysql_stmt_error(stmt)); result = 1; memset(&bind, 0 , sizeof(bind)); bind.buffer_type = MYSQL_TYPE_LONG; bind.is_unsigned = (my_bool)0; bind.is_null = 0; bind.buffer = (char*)&result; bind.buffer_length = 4; bind.length = &length; ret = mysql_stmt_bind_param(stmt, &bind); if(ret) printf("failed to bind param:%s\n", mysql_stmt_error(stmt)); ret = mysql_stmt_execute(stmt); if(ret) printf("failed to execute stmt:%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*)&result; bind.buffer_length = 4; bind.length = &length; ret = mysql_stmt_bind_result(stmt, &bind); if(ret) printf("failed to bind result:%s\n", mysql_stmt_error(stmt)); ret = mysql_stmt_store_result(stmt); if(ret) printf("failed to store result:%s\n", mysql_stmt_error(stmt)); ret = mysql_stmt_fetch(stmt); if(ret) printf("failed to fetch result:%s\n", mysql_stmt_error(stmt)); mysql_stmt_close(stmt); mysql_close(sql); mysql_server_end(); return 0; }