一、运行环境:
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;
}