单线程例子:
案例一:select
#include <mysql.h>
#include <stdio.h>
int main(int argc ,char *argv[]) {
MYSQL * db;
MYSQL_RES * res;
MYSQL_ROW row;
unsigned int nums;
unsigned int i;
if (!(db = mysql_init(NULL))) {
printf("mysql_init:%s\n", mysql_error(db));
exit(1);
}
if (!mysql_real_connect(db, "127.0.0.1", "root", "zhangliang", "test",3306, NULL, 0)) {
printf("mysql_real_connect:%s\n", mysql_error(db));
mysql_close(db);
exit(1);
}
if (mysql_query(db, "select * from t1")) {
printf("myql_query:%s\n", mysql_error(db));
mysql_close(db);
exit(1);
}
res = mysql_store_result(db);
nums = mysql_num_fields(res);
while (row = mysql_fetch_row(res)) {
for (i = 0; i < nums; ++i) {
printf("\t%s", row[i]);
}
printf("\n");
}
mysql_close(db);
return 0;
}
预处理SQL案例
案例一,INSERT
#include <mysql.h>
#include <stdio.h>
#include <string.h>
MYSQL * m_mysql_init(MYSQL * mysql) {
MYSQL * handle;
if (NULL == (handle = mysql_init(mysql))) {
fprintf(stderr, "m_mysql_init failed, out of memory\n");
exit(1);
}
return handle;
}
void m_mysql_close(MYSQL * mysql) {
mysql_close(mysql);
}
MYSQL * handle;
if (NULL == (handle = mysql_real_connect(mysql, host, user, passwd, db, port, unix_sock, client_flag))) {
fprintf(stderr, "Failed to connect databases:Error:%s\n", mysql_error(mysql));
exit(1);
}
return handle;
}
MYSQL_STMT * m_mysql_stmt_init(MYSQL * mysql) {
MYSQL_STMT * stmt;
if (NULL == (stmt = mysql_stmt_init(mysql))) {
fprintf(stderr, "m_mysql_stmt_init, out of memory\n");
exit(1);
}
return stmt;
}
int m_mysql_stmt_prepare(MYSQL_STMT * stmt, const char * stmt_str, unsigned long length) {
int ret;
if (0 != (ret = mysql_stmt_prepare(stmt, stmt_str, length))) {
fprintf(stderr, "m_mysql_stmt_prepare failed, error:%s\n", mysql_stmt_error(stmt));
exit(1);
}
return ret;
}
my_bool m_mysql_stmt_bind_param(MYSQL_STMT * stmt, MYSQL_BIND * bind) {
my_bool ret;
if (0 != (ret = mysql_stmt_bind_param(stmt, bind))) {
fprintf(stderr, "m_mysql_stmt_bind_param, error:%s\n", mysql_stmt_error(stmt));
exit(1);
}
return ret;
}
int m_mysql_stmt_execute(MYSQL_STMT * stmt) {
int ret;
if (0 != (ret = mysql_stmt_execute(stmt))) {
fprintf(stderr, "m_mysql_stmt_execute, error:%s\n", mysql_stmt_error(stmt));
exit(1);
}
return ret;
}
my_bool m_mysql_stmt_close(MYSQL_STMT * stmt) {
return mysql_stmt_close(stmt);
}
#define DROP_TABLE "DROP TABLE IF EXISTS test_table"
#define CREATE_TABLE "CREATE TABLE test_table(a INT, b VARCHAR(40), c SMALLINT, d TIMESTAMP)"
#define INSERT_TABLE "INSERT INTO test_table(a,b,c) VALUES(?,?,?)"
#define STRING_SIZE 50
int main(int argc, char * argv[]) {
MYSQL * handle;
MYSQL_STMT * stmt;
MYSQL_BIND bind[3];
int int_data;
char str_data[STRING_SIZE];
short small_data;
unsigned long str_length;
my_bool is_null;
handle = m_mysql_init(NULL);
m_mysql_real_connect(handle, "127.0.0.1", "root", "zhangliang", "test", 3306, NULL, 0);
mysql_query(handle, DROP_TABLE);
mysql_query(handle, CREATE_TABLE);
stmt = m_mysql_stmt_init(handle);
m_mysql_stmt_prepare(stmt, INSERT_TABLE, strlen(INSERT_TABLE));
bind[0].buffer_type = MYSQL_TYPE_LONG;
bind[0].buffer = (char *)&int_data;
bind[0].is_null = 0;
bind[0].length = 0;
bind[1].buffer_type = MYSQL_TYPE_STRING;
bind[1].buffer = str_data;
bind[1].buffer_length = STRING_SIZE;
bind[1].is_null = 0;
bind[1].length = &str_length;
bind[2].buffer_type = MYSQL_TYPE_SHORT;
bind[2].buffer = (char *)&small_data;
bind[2].is_null = &is_null;
bind[2].length = 0;
m_mysql_stmt_bind_param(stmt, bind);
int_data = 10;
strncpy(str_data, "This is test A", STRING_SIZE);
str_length = strlen(str_data);
is_null = 1;
m_mysql_stmt_execute(stmt);
int_data = 1000;
strncpy(str_data, "This is test B", STRING_SIZE);
str_length = strlen(str_data);
is_null = 0;
small_data = 1000;
m_mysql_stmt_execute(stmt);
m_mysql_stmt_close(stmt);
m_mysql_close(handle);
return 0;
}
获取BLOB或者长字符串的长度用于内存分配
1)在调用mysql_stmt_fetch之前,使用STMT_ATTR_UPDATE_MAX_LENGTH 调用mysql_stmt_attr_set 函数,然后调用mysql_stmt_store_result函数去缓存所有结果,然后mysql_stmt_result_metadata函数返回的结构体成员max_length中含有最大长度的列的长度
2)调用mysql_stmt_fetch,在绑定的结果长度中赋值为0
real_length = 0;
bind[0].buffer = 0;
bind[0].buffer_length = 0;
bind[0].length = &real_length;
mysql_stmt_bind_result(stmt, bind);
mysql_stmt_fetch(stmt);
if (real_length > 0) {
data = malloc(real_length);
bind[0].buffer = data;
bind[0].buffer_length = real_length;
mysql_stmt_fetch(stmt, bind, 0, 0)
}
案例二:select
#include <mysql.h>
#include <stdio.h>
#include <string.h>
MYSQL * m_mysql_init(MYSQL * mysql) {
MYSQL * handle;
if (NULL == (handle = mysql_init(mysql))) {
fprintf(stderr, "m_mysql_init failed, out of memory\n");
exit(1);
}
return handle;
}
void m_mysql_close(MYSQL * mysql) {
mysql_close(mysql);
}
MYSQL * handle;
if (NULL == (handle = mysql_real_connect(mysql, host, user, passwd, db, port, unix_socket, client_flag))) {
fprintf(stderr, "Failed to connect databases:Error:%s\n", mysql_error(mysql));
exit(1);
}
return handle;
}
MYSQL_STMT * m_mysql_stmt_init(MYSQL * mysql) {
MYSQL_STMT * stmt;
if (NULL == (stmt = mysql_stmt_init(mysql))) {
fprintf(stderr, "m_mysql_stmt_init, out of memory\n");
exit(1);
}
return stmt;
}
int m_mysql_stmt_prepare(MYSQL_STMT * stmt, const char * stmt_str, unsigned long length) {
int ret;
if (0 != (ret = mysql_stmt_prepare(stmt, stmt_str, length))) {
fprintf(stderr, "m_mysql_stmt_prepare failed, error:%s\n", mysql_stmt_error(stmt));
exit(1);
}
return ret;
}
my_bool m_mysql_stmt_bind_param(MYSQL_STMT * stmt, MYSQL_BIND * bind) {
my_bool ret;
if (0 != (ret = mysql_stmt_bind_param(stmt, bind))) {
fprintf(stderr, "m_mysql_stmt_bind_param, error:%s\n", mysql_stmt_error(stmt));
exit(1);
}
return ret;
}
my_bool m_mysql_stmt_bind_result(MYSQL_STMT * stmt, MYSQL_BIND * bind) {
my_bool ret;
if (0 != (ret = mysql_stmt_bind_result(stmt, bind))) {
fprintf(stderr, "m_mysql_stmt_bind_result faile, error:%s\n", mysql_stmt_error(stmt));
exit(1);
}
return ret;
}
int m_mysql_stmt_execute(MYSQL_STMT * stmt) {
int ret;
if (0 != (ret = mysql_stmt_execute(stmt))) {
fprintf(stderr, "m_mysql_stmt_execute failed, error:%s\n", mysql_stmt_error(stmt));
exit(1);
}
return ret;
}
MYSQL_RES * m_mysql_stmt_result_metadata(MYSQL_STMT * stmt) {
MYSQL_RES * res;
if (NULL == (res = mysql_stmt_result_metadata(stmt))) {
fprintf(stderr, "m_mysql_stmt_result_metadata failed, erroro:%s\n", mysql_stmt_error(stmt));
exit(1);
}
return res;
}
void m_mysql_free_result(MYSQL_RES * res) {
mysql_free_result(res);
}
int m_mysql_stmt_store_result(MYSQL_STMT * stmt) {
int ret;
if (0 != (ret = mysql_stmt_store_result(stmt))) {
fprintf(stderr, "m_mysql_stmt_store_result failed,error %d\n", mysql_stmt_error(stmt));
exit(1);
}
return ret;
}
unsigned long m_mysql_stmt_param_count(MYSQL_STMT * stmt) {
return mysql_stmt_param_count(stmt);
}
my_bool m_mysql_stmt_close(MYSQL_STMT * stmt) {
return mysql_stmt_close(stmt);
}
unsigned int m_mysql_num_fields(MYSQL_RES * res) {
return mysql_num_fields(res);
}
#define STRING_SIZE 50
#define SELECT_STMT "SELECT * FROM test_table"
int main(int argc, char * argv[]) {
MYSQL * handle;
MYSQL_STMT * stmt;
MYSQL_BIND bind[4];
MYSQL_RES * prepare_meta_result;
MYSQL_TIME ts;
short small_data;
int int_data;
char str_data[STRING_SIZE];
my_bool is_null[4];
unsigned long length[4];
my_bool error[4];
handle = m_mysql_init(NULL);
m_mysql_real_connect(handle, "127.0.0.1", "root", "zhangliang", "test", 3306, NULL, 0);
stmt = m_mysql_stmt_init(handle);
m_mysql_stmt_prepare(stmt, SELECT_STMT, strlen(SELECT_STMT));
printf("param count:%d\n", m_mysql_stmt_param_count(stmt));
prepare_meta_result = m_mysql_stmt_result_metadata(stmt);
printf("total columns in select statement: %d\n", m_mysql_num_fields(prepare_meta_result));
m_mysql_stmt_execute(stmt);
bind[0].buffer_type = MYSQL_TYPE_LONG;
bind[0].buffer = (char *)&int_data;
bind[0].is_null = &is_null[0];
bind[0].length = &length[0];
bind[0].error = &error[0];
bind[1].buffer_type = MYSQL_TYPE_STRING;
bind[1].buffer = (char *)str_data;
bind[1].buffer_length = STRING_SIZE;
bind[1].is_null = &is_null[1];
bind[1].length = &length[1];
bind[1].error = &error[1];
bind[2].buffer_type = MYSQL_TYPE_SHORT;
bind[2].buffer = (char *)&small_data;
bind[2].is_null = &is_null[2];
bind[2].length = &length[2];
bind[2].error = &error[2];
bind[3].buffer_type = MYSQL_TYPE_TIMESTAMP;
bind[3].buffer = (char *)&ts;
bind[3].is_null = &is_null[3];
bind[3].length = &length[3];
bind[3].error = &error[3];
m_mysql_stmt_bind_result(stmt, bind);
while (!mysql_stmt_fetch(stmt)) {
if (is_null[0]) {
printf("NULL\t");
}
else {
printf("%d\t", int_data);
}
if (is_null[1]) {
printf("NULL\t");
}
else {
printf("%s\t", str_data);
}
if (is_null[2]) {
printf("NULL\t");
}
else {
printf("%d\t", small_data);
}
if (is_null[3]) {
printf("NULL\n");
}
else {
printf("%04d-%02d-%02d %02d:%02d:%02d\n", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second);
}
}
m_mysql_free_result(prepare_meta_result);
m_mysql_stmt_close(stmt);
m_mysql_close(handle);
return 0;
}