要想使用mysql, 需要先开启服务端,然后以下代码为客户端,首先连接数据库,然后再进行各种增删改查操作。
本代码不能直接运行
需要链库 -lmysqlclient
#include "mysql.h"
#include
int ConnectDB()
{
// global variable mysql
if (NULL == mysql_init(&mysql)) { //分配和初始化MYSQL对象
printf("mysql_init(): %s\n", mysql_error(&mysql));
return -1;
}
//尝试与运行在主机上的MySQL数据库引擎建立连接
if (NULL == mysql_real_connect(&mysql,
"localhost", // host
"root", // username
"ubuntu", // password
"Xianghan", // DATABASE NAME
0,
NULL,
0)) {
printf("mysql_real_connect(): %s\n", mysql_error(&mysql));
return -1;
}
printf("Connected MySQL successful! \n");
return SUCCESS;
}
{
int rc;
char query_str[500] = {0};
// INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
// query_str = "insert into Sip_channel (%1, %2, %3 ...) values ( % , % , % , % )";
// UPDATE table_name
// SET column1=value1,column2=value2,...
// WHERE some_column=some_value;
// Sip_server
// strcpy(query_str, "UPDATE sip_server "
// "SET server_id=%s," // SipServerID
// "server_domain=%s," // SipServerDomain
// "server_ip=%s, " // SipServerIp
// "server_port=%d," // SipServerPort
// "type=1"
// );
snprintf(query_str, sizeof(query_str),"UPDATE sip_server "
"SET server_id=%s," // SipServerID
"server_domain=%s," // SipServerDomain
"server_ip=\"%s\", " // SipServerIp
"server_port=%d," // SipServerPort
"type=1",
SipServerID, SipServerDomain, SipServerIp, SipServerPort);
printf("query_str = %s\n", query_str);
// execute
rc = mysql_real_query(&mysql, query_str, strlen(query_str));
if (0 != rc) {
printf("mysql_real_query(): %s\n", mysql_error(&mysql));
return -1;
}
// Sip_channel
// strcpy(query_str, "UPDATE sip_channel "
// "SET id=1, "
// "sip_connection_id=%s," //
// "sip_video_channel_id=%s," //
// " civilcode=$d"
// );
// execute
// rc = mysql_real_query(&mysql, query_str, strlen(query_str));
// if (0 != rc) {
// printf("mysql_real_query(): %s\n", mysql_error(&mysql));
// return -1;
// }
// Sip_connection
// strcpy(query_str, "UPDATE sip_connection "
// "SET local_port=%d," // SipLocalPort
// "user_auth_id=%s," // SipUserRegID
// "user_auth_pwd=%s, " // SipUserPasswd
// "valid_reg_period=%d," // valid_reg_period
// "heart_cycle=%d, " // HeartCycle
// "max_heart_timeout=%d," // Hearttime
// "user_auth_name=%d" // SipUserRegName
// );
snprintf(query_str, sizeof(query_str), "UPDATE sip_connection " // 写sql语句
"SET local_port=%d," // SipLocalPort
"user_auth_id=\"%s\"," // SipUserRegID
"user_auth_pwd=%s, " // SipUserPasswd
"valid_reg_period=%d," // valid_reg_period
"heart_cycle=%d, " // HeartCycle
"max_heart_timeout=%d," // Hearttime
"user_auth_name=\"%s\"", // SipUserRegName
SipLocalPort , SipUserRegID, SipUserPasswd, valid_reg_period, HeartCycle, Hearttime, SipUserRegName);
printf("query_str = %s\n", query_str);
// execute
rc = mysql_real_query(&mysql, query_str, strlen(query_str)); // 执行sql语句
if (0 != rc) {
printf("mysql_real_query(): %s\n", mysql_error(&mysql));
return -1;
}
// printf("UPDATE sip_server SET server_domain=%s, server_ip=%s, server_port=%d", SipServerDomain,SipServerIp, SipServerPort);
// printf(query_str,"\n");
// printf(query_str);
// for insert
// res = mysql_store_result(&mysql);
// if (NULL == res) {
// printf("mysql_restore_result(): %s\n", mysql_error(&mysql));
// return -1;
// }
// rows = mysql_num_rows(res);
// printf("The total rows is: %d\n", rows);
// fields = mysql_num_fields(res);
// printf("The total fields is: %d\n", fields);
// while ((row = mysql_fetch_row(res))) {
// for (i = 0; i < fields; i++) {
// printf("%s\t", row[i]);
// }
// printf("\n");
// }
}
int ReleaseDB()
{
mysql_close(&mysql);
return SUCCESS;
}