gpt4 book ai didi

c++ - 在 Windows 7 64 位上使用 MySql 连接器设置 Code::Blocks

转载 作者:可可西里 更新时间:2023-11-01 06:48:30 25 4
gpt4 key购买 nike

这个问题的变体已经被问过很多次了,但我已经搜索了所有我能在网上找到的东西,但仍然没有一个有效的答案,或者至少没有一个有效的答案。

我正在尝试设置 Code::Blocks 以使用 MySql 连接器,以便发布教程 here运行。据我所知,我已尽我所能,但在编译时仍然出现“ undefined reference ”错误。我相当确定这是一个链接错误。

这是我在编译时遇到的错误:

obj\Release\main.o:main.cpp:(.text.startup+0x15e)
undefined reference to _imp__get_driver_instance' error: ld returned 1 exit status

这是我的设置:

操作系统:Windows 7 64位
IDE:代码:: block 16.01

设置->编译器->搜索目录->编译器:

C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\include  
C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\include\cppconn
C:\local\boost_1_61_0

设置->编译器->搜索目录->链接器:

C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\lib\opt  

设置->编译器->链接器设置->链接库:

C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\lib\opt\mysqlcppconn.lib  
C:\Program Files\MySQL\MySQL Connector C++ 1.1.7\lib\opt\mysqlcppconn-static.lib

就像我说的,网络上有很多关于这个特定错误的问题,但我还没有找到任何适合我的设置的解决方案。我承认我对链接库有点生疏,但我发誓我已经尝试了我能想到的所有可能的变体,但都没有成功。

代码如下:

/* Standard C++ headers */
#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <stdexcept>
#include <stdlib.h>

/* MySQL Connector/C++ specific headers */
#include <driver.h>
#include <connection.h>
#include <statement.h>
#include <prepared_statement.h>
#include <resultset.h>
#include <metadata.h>
#include <resultset_metadata.h>
#include <exception.h>
#include <warning.h>

#define DBHOST "removed"
#define USER "removed"
#define PASSWORD "removed"
#define DATABASE "removed"

#define NUMOFFSET 1
#define COLNAME 1

using namespace std;
using namespace sql;

static void retrieve_data_and_print (ResultSet *rs, int type, int colidx, string colname) {

/* retrieve the row count in the result set */
cout << "\nRetrieved " << rs -> rowsCount() << " row(s)." << endl;

cout << "\nTestColumnName" << endl;
cout << "--------" << endl;

/* fetch the data : retrieve all the rows in the result set */
while (rs->next()) {
if (type == NUMOFFSET) {
cout << rs -> getString(colidx) << endl;
} else if (type == COLNAME) {
cout << rs -> getString(colname) << endl;
} // if-else
} // while

cout << endl;
}

int main(int argc, const char *argv[]) {
Driver *driver;
Connection *con;
Statement *stmt;
ResultSet *res;
PreparedStatement *prep_stmt;
Savepoint *savept;

int updatecount = 0;

/* initiate url, user, password and database variables */
string url(argc >= 2 ? argv[1] : DBHOST);
const string user(argc >= 3 ? argv[2] : USER);
const string password(argc >= 4 ? argv[3] : PASSWORD);
const string database(argc >= 5 ? argv[4] : DATABASE);

try {
driver = get_driver_instance();

/* create a database connection using the Driver */
con = driver -> connect(url, user, password);

/* turn off autocommit */
con -> setAutoCommit(0);

cout << "Database connection\'s autocommit mode = " << con -> getAutoCommit() << endl;

// select database schema
con -> setSchema(database);

// create a statement object
stmt = con -> createStatement();

cout << "Executing Query: \"SELECT * FROM organizations\" ... " << endl;

/* run query */
res = stmt -> executeQuery ("SELECT * FROM organizations");

cout << "Retrieving the result set ..." << endl;
retrieve_data_and_print (res, NUMOFFSET, 1, string("TestColumnName"));

} catch (SQLException &e) {
cout << "ERROR: SQLException in " << __FILE__;
cout << " (" << __func__<< ") on line " << __LINE__ << endl;
cout << "ERROR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << ")" << endl;

if (e.getErrorCode() == 1047) {
/*
Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR)
Message: Unknown command
*/
cout << "\nYour server does not seem to support Prepared Statements at all. ";
cout << "Perhaps MYSQL < 4.1?" << endl;
}

return EXIT_FAILURE;
} catch (std::runtime_error &e) {

cout << "ERROR: runtime_error in " << __FILE__;
cout << " (" << __func__ << ") on line " << __LINE__ << endl;
cout << "ERROR: " << e.what() << endl;

return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

最佳答案

由于链接器不正确,我遇到了同样的错误,也许您的链接器不正确。使用这些链接器

-lmysqlpp -lmysqlclient

也可以关注this tutorial我在 Eclipse (Ubuntu) 上写的。希望它也能与代码块一起使用。

关于c++ - 在 Windows 7 64 位上使用 MySql 连接器设置 Code::Blocks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39108338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com