Include header file
#include <mysql_connection.h><mysql_driver.h><cppconn/statement.h> <cppconn/resultset.h><cppconn/exception.h>#ifdef _debug#pragma comment ( LIB, "Mysqlcppconn.lib")#else#pragma comment (lib, "Mysqlcppconn-static.lib")// My MySQL connector/c++ is self-compiled under the source code, need to introduce this, the official directly provided by the binary I don't know need not need #pragma comment (lib, "Mysqlclient.lib") #endif
Code
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 ()); }
Static links
1>testdlg.obj:error LNK2001: unresolved external symbols"__declspec (dllimport) class Sql::mysql::mysql_driver * __cdecl sql::mysql::get_driver_instance (void)"([email protected]@[email protected]@[email protected] A@XZ)1>testdlg.obj:error LNK2001: unresolved external symbols"__declspec (dllimport) public:virtual __thiscall sql::sqlexception::~sqlexception (void)"(__imp_??[email protected]@@[email protected])1>testdlg.obj:error LNK2001: unresolved external symbols"__declspec (dllimport) public:int __thiscall sql::sqlexception::geterrorcode (void) const"(__imp_?)[email protected]@[email protected] @QBEHXZ)1>testdlg.obj:error LNK2001: unresolved external symbols"__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: unresolved external symbols"__declspec (dllimport) Public:char const * __thiscall sql::sqlstring::c_str (void) const"(__imp_?)[email protected]@[email protected] @QBEPBDXZ)1>testdlg.obj:error LNK2001: unresolved external symbols"__declspec (dllimport) public: __thiscall sql::sqlstring::sqlstring (char const * const)"(__imp_??[email protected]@@[email protected]@z]1>testdlg.obj:error LNK2001: unresolved external symbols"__declspec (dllimport) public: __thiscall sql::sqlstring::~sqlstring (void)"([email protected]@@[email protected])
By observing Cppconn/build_config.h know to get the introduction of non- __declspec (dllimport) need to define macro Cppconn_lib_build
Solution Solutions
Define the macro Cppconn_lib_build before including the header file, and tell the linker that MySQL connector is compiled by the static library method
#ifndef _debug#define cppconn_lib_build#endif<mysql_connection.h><mysql_driver.h ><cppconn/statement.h><cppconn/resultset.h><cppconn/exception.h >#ifdef _debug#pragma comment (lib, "Mysqlcppconn.lib")#else#pragma comment ( LIB, "Mysqlcppconn-static.lib")// my MySQL connector/c++ is compiled by myself under the source code, need to introduce this, the official directly provided binary I don't know need not need #pragma comment (lib, "Mysqlclient.lib")#endif
Using MySQL connector/c++ 1.1.4 static link error in vc++2013