mysql c&c++ api
基于 mac os环境
Download Connector/C++
https://dev.mysql.com/downloads/connector/cpp/
Header
#include <mysql/mysql.h>
Compiler Arguments
g++ main.cpp `mysql_config --libs`
/*在unix环境中反引号 `` 和 $() 都是命令替换。
在本机(Mac)上直接输入 mysql_config --libs 产生的结果是:
-L/usr/local/Cellar/mysql/5.7.21/lib -lmysqlclient -lssl -lcrypto
所以等于是参数直接替换为后者,两者是相同效果。
同理 mysql_config --cflags
-I/usr/local/Cellar/mysql/5.7.21/include
*/
注:18.9.30更新macOS Mojave 后,编译选项报错,所以我只用了 -lmysqlclient ,把 -lssl -lcrypto 删了,然后未发生错误。
Clion 只需要添加两句
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I/usr/local/Cellar/mysql/5.7.21/include")
add_executable(Project01 main.c)
target_link_libraries(Project01 -L/usr/local/Cellar/mysql/5.7.21/lib -lmysqlclient)
Use in Program
//连接数据库,成功返回0
int connectMysql(MYSQL * mysql){
int ret = 0;
MYSQL *con = NULL;
con = mysql_init(mysql);
if (con == NULL) {
ret = mysql_errno(mysql);
printf("func mysql_init() err :%d\n", ret);
return ret;
}
//连接mysql服务器
//MYSQL *mysql_real_connect(MYSQL *mysql, const char *host, const char *user, const char *passwd,
//const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag) ;
con = mysql_real_connect(mysql, MYSQLHOST, MYSQLUSR, MYSQLPAS, MYSQLDB, 0, NULL, 0 );
if (con == NULL) {
ret = mysql_errno(mysql);
printf("func mysql_real_connect() err :%d\n", ret);
return ret;
}
#ifdef NOTDEFMYSQL
else {
printf("func mysql_real_connect() ok\n");
}
#endif
return 0;
}
/*插入名字和密码,成功则返回0*/
int insertNamePass(MYSQL* mysql, const char* name, const char* pass){
char query[51 + MAXNAME + MAXPASS] = "insert into users(username,password) values(\"";
strncat(query, name, MAXNAME);
strcat(query, "\",\"");
strncat(query, pass, MAXPASS);
strcat(query, "\")");
return mysql_real_query(mysql, query, (unsigned int)strlen(query));
}
/*如果名字存在则返回0*/
int hasName(MYSQL* mysql, const char * name){
char query[38 + MAXNAME] = "SELECT 1 FROM users WHERE username=\"";
strncat(query, name, MAXNAME);
strcat(query, "\"");
if (mysql_real_query(mysql, query, (unsigned int)strlen(query) ) )
{
puts("MySQL query is error!");
}
else{
MYSQL_RES *result = mysql_store_result(mysql);
if( mysql_num_rows(result) ){
mysql_free_result(result);
return 0;
}
}
return -1;
}
//检测用户的密码是否匹配,匹配则返回0
int passRight(MYSQL* mysql, const char * name, const char * pass){
char query[45 + MAXNAME] = "select password from users where username=\"";
strncat(query, name, MAXNAME);
strcat(query, "\"");
if (mysql_real_query(mysql, query, (unsigned int)strlen(query) ) )
{
puts("MySQL query is error!");
}
else{
MYSQL_RES *result = mysql_store_result(mysql);
MYSQL_ROW row = mysql_fetch_row(result);
if( !strncmp(row[0], pass, MAXPASS)){
mysql_free_result(result);
return 0;
}
mysql_free_result(result);
}
return -1;
}
//返回用户分数,2 Bytes
short getGrade(MYSQL* mysql, const char * name){
char query[42 + MAXNAME] = "select grade from users where username=\"";
strncat(query, name, MAXNAME);
strcat(query, "\"");
if (mysql_real_query(mysql, query, (unsigned int)strlen(query) ) )
{
puts("MySQL query is error!");
}
else{
MYSQL_RES *result = mysql_store_result(mysql);
MYSQL_ROW row = mysql_fetch_row(result);
short grade = (short)strtol(row[0], NULL, 10);
mysql_free_result(result);
return grade;
}
return -1;
}