VC++2010配置使用MySQL5.6


 

 

0、前提

    安裝后的文件概覽

    

 

 

    編譯器:  VC++2010

 

    MySQL版本:MySQL5.6.19 for win64     

    

 

    Connector版本:connector  c++  1.1.3

    

 

 

 

在VS2010下配置使用MySQL

 

1、配置頭文件

    項目屬性--VC++目錄--包含目錄

    

 

 

2、配置庫文件

    在connector c++ 1.1.3\lib目錄下有兩個目錄:debug目錄 和 opt目錄

    

 

 

    lib\debug目錄

    

 

 

 

    lib\opt目錄

    

 

    由於有debug目錄,所以猜測opt目錄可能是類似release目錄的優化(optimize)后的文件,因此在VC++中使用時在Debug下使用debug目錄下的庫文件,在Release模式下使用opt目錄下的庫目錄。

 

    eg.

    #ifdef  _DEBUG

    #pragma   comment(lib, "debug下的mysqlcppconn.lib")

    #pragma   comment(lib, "debug下的mysqlcppconn-static.lib")

    #else 

    #pragma   comment(lib, "opt下的mysqlcppconn.lib")

    #pragma   comment(lib, "opt下的mysqlcppconn-static.lib")

    #endif

 

    另外,在Debug或Release模式下將debug或opt目錄下的mysqlcppcon.dll拷貝到項目目錄下或system32目錄下。  將 MySQL\MySQL Server5.6\lib目錄下的libmysql.dll拷貝到項目目錄下或system32目錄下。

    

 

    

3、配置項目

    由於該版本的MySQL是64位的,因此使用該MySQL的connector的項目必須被配置為X64類型的。 否則會有鏈接錯誤! 這一點要注意!

    

 

4、Demo

    數據庫:db_1220, 表:tbl_user,  MySQL服務器:本地的localhost

    

 

    

#include "stdafx.h" #include <Windows.h> #include <iostream> #include <mysql_connection.h> #include <cppconn/driver.h> #include <cppconn/exception.h> #include <cppconn/resultset.h> #include <cppconn/statement.h>
#pragma warning(disable:4251) #ifdef _DEBUG #pragma comment(lib, "D:\\Program Files\\MySQL\\Connector C++ 1.1.3\\lib\\debug\\mysqlcppconn-static.lib")
#pragma comment(lib, "D:\\Program Files\\MySQL\\Connector C++ 1.1.3\\lib\\debug\\mysqlcppconn.lib")
#else
#pragma comment(lib, "D:\\Program Files\\MySQL\\Connector C++ 1.1.3\\lib\\opt\\mysqlcppconn-static.lib")
#pragma comment(lib, "D:\\Program Files\\MySQL\\Connector C++ 1.1.3\\lib\\opt\\mysqlcppconn.lib")
#endif



using namespace std; int _tmain(int argc, _TCHAR* argv[]) { sql::Driver *driver = NULL; sql::Connection *con    = NULL; sql::Statement *stmt   = NULL; sql::ResultSet *res    = NULL; sql::SQLString strHost("localhost"); sql::SQLString strUser("root"); sql::SQLString strPwd("XXXXXXX"); sql::SQLString strSchema("db_1220"); sql::SQLString strQuery("select * from tbl_user"); try { driver = get_driver_instance(); con = driver->connect(strHost, strUser, strPwd); con->setSchema(strSchema); stmt = con->createStatement(); res = stmt->executeQuery(strQuery); sql::ResultSetMetaData* pMetaData = res->getMetaData(); cout << endl; cout << "Results have " << res->rowsCount() << " rows" << endl << endl; while(res->next()) { //get data by column name
            cout << res->getInt("id") << "   "
                 << res->getString("name").c_str()   //sql::SQLString沒有重載<<操作符,因此不能直接cout<<res->getString("name")
                 << "   "
                 << res->getString("password").c_str() << endl; //get data by column index
            cout << res->getInt(1) << "   "
                 << res->getString(2).c_str() << "   "
                 << res->getString(3).c_str() << endl; } } catch (sql::SQLException& e) { cerr << endl << e.what() << endl; } catch (...) { cerr << endl << "some exception happeded" << endl; } if (NULL != res) delete res; if (NULL != stmt) delete stmt; if (NULL != con) delete con; cout << endl << endl; return 0; }

 

    

 

運行結果:

 

 

 

 5、補充

     如果在編譯過程中報錯找不到類似 “<boost/variant.hpp>”這樣的錯誤信息,則是需要boost庫支持,下載boost庫配置一下即可。

  

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM