Reputation: 127
everyone. I want to use C++ to connect Mysql, but it doesn't work: the error information are:
error LNK2019: unresolved external symbol "__declspec(dllimport) public: char const * __thiscall sql::SQLString::c_str(void)const " (__imp_?c_str@SQLString@sql@@QBEPBDXZ) referenced in function __catch$?RunConnectMySQL@@YAXXZ$0
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sql::SQLString::~SQLString(void)" (__imp_??1SQLString@sql@@QAE@XZ) referenced in function "void __cdecl RunConnectMySQL(void)" (?RunConnectMySQL@@YAXXZ)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sql::SQLString::SQLString(char const * const)" (__imp_??0SQLString@sql@@QAE@QBD@Z) referenced in function "void __cdecl RunConnectMySQL(void)" (?RunConnectMySQL@@YAXXZ)
error LNK2019: unresolved external symbol "__declspec(dllimport) class sql::mysql::MySQL_Driver * __cdecl sql::mysql::get_driver_instance(void)" (__imp_?get_driver_instance@mysql@sql@@YAPAVMySQL_Driver@12@XZ) referenced in function "class sql::mysql::MySQL_Driver * __cdecl sql::mysql::get_mysql_driver_instance(void)" (?get_mysql_driver_instance@mysql@sql@@YAPAVMySQL_Driver@12@XZ)
And I have search the same question in stackoverflow, but it hasn't a proper answer. and My code is:
#include <iostream>
#include <string>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
using namespace std;
void RunConnectMySQL()
{
sql::mysql::MySQL_Driver *driver = NULL;
sql::Connection *con = NULL;
Statement *state = NULL;
ResultSet *result = NULL;
try
{
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306","root","");
state = con->createStatement();
state->execute("use monitor");
result = state->executeQuery("select * from address");
}
catch(sql::SQLException & ex)
{
cout<<ex.what()<<endl;
return;
}
while(result->next())
{
cout<<"source: "<<result->getString("source").c_str()<<endl;
}
state->close();
}
int main()
{
RunConnectMySQL();
system("pause");
return 0;
}
Thanks.
Upvotes: 1
Views: 1504
Reputation: 1927
You need to link the MySQL connector library to your executable. Before doing this you need to decide which kind of linking you should choice. MySQL connector supports two different kind of linking: static or dynamic.
On the official website there is a tutorial which shows how to create a simple project in Netbeans with both the type of linking. I don't know if you use Netbeans but I'm pretty sure that you will be able to replicate the described operations on any other IDE.
Finally, be sure that you have installed appropriately the MySQL connector. Have a look to the official documentation in order to understand how to do this for your operating system.
Upvotes: 2