今天在看mysql connector C++ 在测试运行是总是出错 求大神看看这是什么情况
今天在看mysql connector C++ 在测试运行是老是出错 求大神看看这是什么情况
测试代码是直接网上拉来到,运行是老是出现如下错误:
1.undefined reference to `sql::mysql::get_driver_instance()'
2.collect2: ld returned 1 exit status
------解决方案--------------------
-lmysql
#include <iostream>
#include <mysql_driver.h>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>
using namespace sql;
using namespace std;
void RunConnectMySQL()
{
mysql::MySQL_Driver *driver;
Connection *con;
Statement *state;
ResultSet *result;
// 初始化驱动
driver = sql::mysql::get_driver_instance();
// 建立链接
con = driver->connect("tcp://127.0.0.1:3306", "root", "");
state = con->createStatement();
state->execute("use test");
// 查询
result = state->executeQuery("select * from sms_set");
// 输出查询
while(result->next())
{
int id = result->getInt("sms_id");
cout << id << " : " << endl;
}
delete state;
delete con;
}
int main()
{
RunConnectMySQL();
//getchar();
return 0;
}
测试代码是直接网上拉来到,运行是老是出现如下错误:
1.undefined reference to `sql::mysql::get_driver_instance()'
2.collect2: ld returned 1 exit status
------解决方案--------------------
-lmysql