mysql api
MYSQL mysql;
mysql_init(&mysql);
std::string host = "127.0.0.1";
std::string user = "root";
std::string pswd = "admin";
std::string dbName = "qiandaohu";
unsigned int port = 3306;
if (mysql_real_connect(&mysql, host.data(), user.data(), pswd.data(), dbName.data(), port, NULL, 0))
{
mysql_query(&mysql, "SET NAMES GBK"); //设置编码格式
std::cout << "mysql connected!" << std::endl;
}
else
{
std::cout << "mysql connect failed!" << mysql_error(&mysql) << std::endl;
}
int res = mysql_query(&mysql, "select * from role");//查询 if (!res) { MYSQL_RES *result = mysql_store_result(&mysql); if (result) { /*MYSQL_FIELD* sql_field = NULL; while (sql_field = mysql_fetch_field(result)) std::cout << sql_field->name << "\t"; unsigned int cols = mysql_num_fields(result);*/ MYSQL_ROW sql_row; while (sql_row = mysql_fetch_row(result))//获取具体的数据 { RoleDB role; role.type = RoleType(atoi(sql_row[0])); role.name = sql_row[1]; role.hp = atoi(sql_row[2]); roles.push_back(role); /*int idx = 0; while (idx < cols) std::cout << sql_row[idx++] << "\t"; std::cout << std::endl;*/ } mysql_free_result(result); } } else { std::cout << "query sql failed!" << mysql_error(&mysql) << std::endl; }