vs2008 C++项目连接mysql数据库详解

一、运行环境:
Win7 32bit
Vsiual C++ 2010、
MySQL5.6、
MySQL Connector C++ 1.1.6、 //安装Windows 32位版本的MySQL5.6的连接器
boost_1_58_0 //在“MySQL Connector C++ 1.1.6”中用到。
二、参考文章:
VS2008下C++连接Mysql      http://blog.sina.com.cn/s/blog_7929e19f0101l0oi.html
http://dev.mysql.com/doc/connector-cpp/en/index.html    //MySQL Connector C++ 的参考文档、
//mysql-connector-cpp-en.a4.pdf 下载的PDF官方文档
三、编译完成后加入官方文档中的例子代码:
#include "stdafx.h"

#include<stdlib.h>
#include<iostream>

//下面是mysql的依赖头文件
#include "mysql_connection.h"

#include <cppconn\driver.h>
#include <cppconn\exception.h>
#include <cppconn\sqlstring.h>
#include<cppconn\resultset.h>
#include <cppconn\statement.h>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	cout << endl;
	cout << "Running 'SELECT' Hello World!' \
		AS _message'..." << endl;

	try {
		sql::Driver *driver;
		sql::Connection *con;
		sql::Statement *stmt;
		sql::ResultSet *res;

		/* Create a connection */
		driver = get_driver_instance();
		con = driver->connect("tcp://127.0.0.1:3306", "root", "root");
		/* Connect to the MySQL test database */
		con->setSchema("test");

		stmt = con->createStatement();
		res = stmt->executeQuery("SELECT 'Hello World!' AS _message");
		while (res->next()) {
			cout << "\t... MySQL replies: ";
			/* Access column data by alias or column name */
			cout << res->getString("_message") << endl;
			cout << "\t... MySQL says it again: ";
			/* Access column fata by numeric offset, 1 is the first column */
			cout << res->getString(1) << endl;
		}
		delete res;
		delete stmt;
		delete con;
	} catch (sql::SQLException &e) {
		cout << "# ERR: SQLException in " << __FILE__;
		cout << "(" << __FUNCTION__ << ") on line " \
			<< __LINE__ << endl;
		cout << "# ERR: " << e.what();
		cout << " (MySQL error code: " << e.getErrorCode();
		cout << ", SQLState: " << e.getSQLState() << " )" << endl;
	}
	cout << endl;

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值