143 lines
3.3 KiB
C
143 lines
3.3 KiB
C
#include "db.h"
|
||
#include <stdio.h>
|
||
|
||
int connect_mysql(const char *host, int port, const char *user, const char *pwd, const char *db_name)
|
||
{
|
||
if (conn_db == NULL)
|
||
{
|
||
conn_db = (MYSQL *)malloc(sizeof(MYSQL));
|
||
if (mysql_init(conn_db) != NULL)
|
||
{
|
||
mysql_set_character_set(conn_db, "utf8");
|
||
// if (mysql_real_connect(conn_db, "localhost", "disen", "disen", "db1",
|
||
// 3306, NULL, 0) == NULL)
|
||
if (mysql_real_connect(conn_db, host, user, pwd, db_name,
|
||
port, NULL, 0) == NULL)
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
return -1;
|
||
}
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
int query(const char *sql, void (*callback)(MYSQL_ROW row, char (*columns)[30], int cols))
|
||
{
|
||
if (NULL == conn_db)
|
||
{
|
||
return -1;
|
||
}
|
||
int ret = mysql_real_query(conn_db, sql, strlen(sql));
|
||
if (ret != 0)
|
||
{
|
||
return -1;
|
||
}
|
||
|
||
MYSQL_RES *res = mysql_store_result(conn_db);
|
||
if (res != NULL)
|
||
{
|
||
my_ulonglong rows = mysql_num_rows(res);
|
||
// printf("查找到的数据库的行数为 %llu\n", rows);
|
||
unsigned int cols = mysql_num_fields(res);
|
||
char colunm_names[cols][30];
|
||
int i = 0;
|
||
|
||
MYSQL_FIELD *field = NULL;
|
||
while ((field = mysql_fetch_field(res)) != NULL)
|
||
{
|
||
strcpy(colunm_names[i++], field->name);
|
||
}
|
||
|
||
for (int i = 0; i < rows; i++)
|
||
{
|
||
MYSQL_ROW row = mysql_fetch_row(res);
|
||
if (callback != NULL)
|
||
callback(row, colunm_names, cols);
|
||
}
|
||
mysql_free_result(res);
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
int insert(const char *sql, MYSQL_BIND *params)
|
||
{
|
||
MYSQL_STMT *stmt = mysql_stmt_init(conn_db);
|
||
mysql_stmt_prepare(stmt, sql, strlen(sql));
|
||
|
||
if (params != NULL)
|
||
{
|
||
if (mysql_stmt_bind_param(stmt, params)) // 11)错误 0)成功
|
||
{
|
||
mysql_stmt_close(stmt);
|
||
return -1;
|
||
}
|
||
}
|
||
if (mysql_stmt_execute(stmt) != 0)
|
||
{
|
||
mysql_stmt_close(stmt);
|
||
return -1;
|
||
}
|
||
int ret = mysql_stmt_fetch(stmt);
|
||
mysql_stmt_close(stmt);
|
||
return ret; // 返回是影响的行数
|
||
}
|
||
|
||
int delete(const char *sql, MYSQL_BIND *params)
|
||
{
|
||
MYSQL_STMT *stmt = mysql_stmt_init(conn_db);
|
||
mysql_stmt_prepare(stmt, sql, strlen(sql));
|
||
|
||
if (params != NULL)
|
||
{
|
||
if (mysql_stmt_bind_param(stmt, params)) // 11)错误 0)成功
|
||
{
|
||
mysql_stmt_close(stmt);
|
||
return -1;
|
||
}
|
||
}
|
||
if (mysql_stmt_execute(stmt) != 0)
|
||
{
|
||
mysql_stmt_close(stmt);
|
||
return -1;
|
||
}
|
||
int ret = mysql_stmt_fetch(stmt);
|
||
mysql_stmt_close(stmt);
|
||
return ret; // 返回是影响的行数
|
||
}
|
||
|
||
int close_mysql()
|
||
{
|
||
if (conn_db != NULL)
|
||
{
|
||
mysql_close(conn_db);
|
||
return 0;
|
||
}
|
||
return -1;
|
||
}
|
||
|
||
int result_rows(const char *sql)
|
||
{
|
||
if (NULL == conn_db)
|
||
{
|
||
return -1;
|
||
}
|
||
int ret = mysql_real_query(conn_db, sql, strlen(sql));
|
||
if (ret != 0)
|
||
{
|
||
return -1;
|
||
}
|
||
my_ulonglong rows = 0;
|
||
MYSQL_RES *res = mysql_store_result(conn_db);
|
||
if (res != NULL)
|
||
{
|
||
rows = mysql_num_rows(res);
|
||
// printf("查找到的数据库的行数为 %llu\n", rows);
|
||
}
|
||
|
||
return rows;
|
||
} |