#include <iostream>
#include <mysql.h>
using namespace std;
int main(){
const char * host = "localhost";
const char * user = "root";
const char * pass = "";
const char * db = "test";
//定义mysql变量
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;
//初始化
mysql_init(&mysql);
//连接数据库
if (!mysql_real_connect(&mysql, host, user, pass, db, 0, NULL, 0)){
cout << mysql_error(&mysql) << endl;
exit(1);
}
//查询
const char* sql = "select uid, tm from GIFT_RECORD";
if (mysql_query(&mysql, sql) != 0){
cout << "errno:" << mysql_errno(&mysql) << ", error:" << mysql_error(&mysql) << endl;
exit(1);
}
//获取结果
res = mysql_store_result(&mysql);
while ((row = mysql_fetch_row(res))){
cout << "uid:" << row[0] << ", tm:" << row[1] << endl;
}
//释放结果集
mysql_free_result(res);
//关闭连接
mysql_close(&mysql);
cout << "finish" << endl;
return 0;
}
mysql c++代码
最新推荐文章于 2019-07-08 18:19:08 发布