標籤:style blog http io color os ar 使用 for
包含標頭檔
#include <mysql_connection.h>#include <mysql_driver.h>#include <cppconn/statement.h>#include <cppconn/resultset.h>#include <cppconn/exception.h>#ifdef _DEBUG#pragma comment(lib, "mysqlcppconn.lib")#else#pragma comment(lib, "mysqlcppconn-static.lib")// 我的MySQL connector/C++是自己下源碼編譯的,需要引入這個,官方直接提供的二進位我不清楚需要不需要#pragma comment(lib, "mysqlclient.lib")#endif
代碼
try { const char* user = "root"; const char* passwd = "123456"; const char* host = "tcp://192.168.1.8:3306"; const char* database = "mysql"; sql::mysql::MySQL_Driver* driver = sql::mysql::get_driver_instance(); sql::Connection* conn = driver->connect(host, user, passwd); conn->setSchema(database); sql::Statement *stmt = conn->createStatement(); sql::ResultSet *res = stmt->executeQuery("select * from user;"); while (res->next()) { AfxMessageBox((res->getString(2) + " | " + res->getString(3)).c_str()); } delete res; delete stmt; delete conn; } catch (sql::SQLException e) { CString strErrorMsg; strErrorMsg.Format("MySQL error code %d: %s, %s", e.getErrorCode(), e.what(), e.getSQLState().c_str()); AfxMessageBox(strErrorMsg); if (e.getErrorCode() == 1047) { AfxMessageBox("1047"); } } catch (std::runtime_error e) { AfxMessageBox(e.what()); }
靜態連結
1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) class sql::mysql::MySQL_Driver * __cdecl sql::mysql::get_driver_instance(void)" ([email protected]@[email protected]@[email protected]12@XZ)1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: virtual __thiscall sql::SQLException::~SQLException(void)" (__imp_??[email protected]@@[email protected])1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: int __thiscall sql::SQLException::getErrorCode(void)const " (__imp_?[email protected]@[email protected]@QBEHXZ)1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __thiscall sql::SQLException::getSQLState(void)const " ([email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@2@@[email protected]@XZ)1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: char const * __thiscall sql::SQLString::c_str(void)const " (__imp_?[email protected]@[email protected]@QBEPBDXZ)1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: __thiscall sql::SQLString::SQLString(char const * const)" (__imp_??[email protected]@@[email protected]@Z)1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: __thiscall sql::SQLString::~SQLString(void)" ([email protected]@@[email protected])
通過觀察cppconn/build_config.h得知若要得到非__declspec(dllimport)方式的引入需要定義宏CPPCONN_LIB_BUILD
解決方案
在包含標頭檔前定義宏CPPCONN_LIB_BUILD,告訴連結器MySQL connector是靜態庫方式編譯的即可
#ifndef _DEBUG#define CPPCONN_LIB_BUILD#endif#include <mysql_connection.h>#include <mysql_driver.h>#include <cppconn/statement.h>#include <cppconn/resultset.h>#include <cppconn/exception.h>#ifdef _DEBUG#pragma comment(lib, "mysqlcppconn.lib")#else#pragma comment(lib, "mysqlcppconn-static.lib")// 我的MySQL connector/C++是自己下源碼編譯的,需要引入這個,官方直接提供的二進位我不清楚需要不需要#pragma comment(lib, "mysqlclient.lib")#endif
vc++2013中使用MySQL connector/C++ 1.1.4靜態連結報錯