包含頭文件
#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)" (__imp_?get_driver_instance@mysql@sql@@YAPAVMySQL_Driver@12@XZ) 1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: virtual __thiscall sql::SQLException::~SQLException(void)" (__imp_??1SQLException@sql@@UAE@XZ) 1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: int __thiscall sql::SQLException::getErrorCode(void)const " (__imp_?getErrorCode@SQLException@sql@@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 " (__imp_?getSQLState@SQLException@sql@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) 1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: char const * __thiscall sql::SQLString::c_str(void)const " (__imp_?c_str@SQLString@sql@@QBEPBDXZ) 1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: __thiscall sql::SQLString::SQLString(char const * const)" (__imp_??0SQLString@sql@@QAE@QBD@Z) 1>TestDlg.obj : error LNK2001: 無法解析的外部符號 "__declspec(dllimport) public: __thiscall sql::SQLString::~SQLString(void)" (__imp_??1SQLString@sql@@QAE@XZ)
通過觀察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
mysql一般是結合php用的..asp 可以用mssql
當然asp也可以用mysql,,先要建立系統DSN
strConnection = "dsn=xxx;driver={myodbd driver};server=localhost;uid=root;pwd=;database=xxx"
Set adoDataConn = Server.CreateObject("ADODB.Connection")
adoDataConn.Open strConnection
在-DBOOST_ROOT變量前或後加上-D MYSQL_DIR="D:\mysql-5.6.10-win32" -D MYSQL_INCLUDE_DIR="D:\mysql-5.6.10-win32\include" 確保變量被引用