MySQL C++连接器错误
各位。我想用C++连接MySQL,但它不起作用: 错误信息包括:MySQL C++连接器错误,c++,mysql,C++,Mysql,各位。我想用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$?RunConnectMySQ
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;
}
谢谢。您需要将MySQL连接器库链接到可执行文件。在此之前,您需要决定应该选择哪种链接。MySQL连接器支持两种不同的链接:静态链接和动态链接 在上有一个教程,它展示了如何在Netbeans中创建一个简单的项目,该项目包含两种类型的链接。我不知道您是否使用Netbeans,但我非常确定您将能够在任何其他IDE上复制所描述的操作 最后,确保已正确安装MySQL连接器。查看以了解如何为您的操作系统执行此操作