大家。我想使用C ++连接Mysql,但它不起作用: 错误信息是:
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)
我在stackoverflow中搜索了同样的问题,但它没有正确的答案。我的代码是:
#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;
}
感谢。
答案 0 :(得分:2)
您需要将MySQL连接器库链接到可执行文件。在此之前,您需要决定选择哪种链接。 MySQL连接器支持两种不同的链接:静态或动态。
在official website上有一个教程,展示了如何在Netbeans中创建一个具有链接类型的简单项目。我不知道你是否使用Netbeans,但我很确定你能够在任何其他IDE上复制所描述的操作。
最后,请确保已正确安装MySQL连接器。请查看official documentation以了解如何为您的操作系统执行此操作。