VC简单操作mysql

#include <iostream>
#include <winsock.h>
#include <mysql.h>
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "libmysql.lib")
using namespace std;

int main()
{
	//cout << mysql_get_client_info() << endl;//Mysql客户端版本号
	MYSQL mydata;
	MYSQL_RES *result;
	MYSQL_ROW row;
	int num, i;
	mysql_library_init(0, NULL, NULL);//初始化MySQL C API库
	mysql_init(&mydata);//初始化mydata的数据结构,mydata是MYSQL对象
	mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk");//连接选项:添加GBK字符集支持

	//连接函数
	if(mysql_real_connect(&mydata, "localhost", "root", "lgh123", "weather", 3306, NULL, 0) != NULL)
	{
		cout << "database connection OK!" << endl;
		mysql_query(&mydata, "select * from weather");
		result = mysql_store_result(&mydata);
		num = mysql_num_fields(result);
		while (row = mysql_fetch_row(result))
		{
			for (i = 0; i < num; i++)
			{
				if (row[i] != NULL)
				{
					cout << row[i] << "  ";
				}
			}
			cout << endl;
		}
		mysql_free_result(result);
	}

	else
	{
		cout << "error" << endl;
	}
	mysql_close(&mydata);
	mysql_library_end();
	return 0;
}
VC简单操作mysql_第1张图片

你可能感兴趣的:(VC简单操作mysql)