#include <iostream>
#include <mysql.h>
using namespace std;
int main()
{
MYSQL ceshi;
mysql_init(&ceshi);
MYSQL_RES *result;
MYSQL_ROW row;
int num;
int i;
if (mysql_real_connect(&ceshi, "127.0.0.1", "root", "123", "carinfo", 3306, NULL, 0)) //连接到MySQL
{
cout << "\n\t-----MySQL已连接-----" << endl;
}
if(!mysql_query(&ceshi, "SELECT * FROM vips")) //若查询成功返回0,失败返回随机数
{
cout << "\n\t ----查询成功---" << endl;
}
result = mysql_store_result(&ceshi) //将查询结果集存储到result中
num = mysql_num_fields(result); //将结果集列数存放到num中
while ((row = mysql_fetch_row(result))) //遇到最后一行,则终止循环
{
for (i = 0; i < num; i++)
{
cout << row[i] << "\t";
}
cout << endl;
}
mysql_free_result(result); //释放结果集所占用的内存
mysql_close(&ceshi); //关闭与mysql的连接
}
MYSQL数据库C++例程代码真好用
最新推荐文章于 2024-08-03 23:00:42 发布