简体   繁体   English

在Windows 7 64位上使用MySql连接器设置代码::块

[英]Setting Up Code::Blocks with MySql Connector on Windows 7 64bit

Variations of this question has been asked many times before, but I've searched through everything I can find on the web and still don't have a valid answer, or at least one that works. 之前已经多次询问过这个问题的变化,但是我搜索了我在网上找到的所有内容,但仍然没有有效的答案,或者至少有一个有效的答案。

I am trying to setup Code::Blocks to use MySql Connector in order to get the tutorial posted here to run. 我正在尝试设置Code :: Blocks以使用MySql Connector,以便在此处发布教程来运行。 I have done everything to the best of my knowledge but still get an 'undefined reference' error on compile. 我尽我所知完成了所有工作,但在编译时仍然遇到“未定义的引用”错误。 I'm fairly certain it is a linking error. 我很确定这是一个链接错误。

Here is the error I get on compile: 这是我在编译时遇到的错误:

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

Here is my setup: 这是我的设置:

OS: Windows 7 64bit 操作系统:Windows 7 64位
IDE: Code::Blocks 16.01 IDE:Code :: Blocks 16.01

Settings->Compiler->Search Directories->Compiler : 设置 - >编译器 - >搜索目录 - >编译器

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  

Settings->Compiler->Search Directories->Linker : 设置 - >编译器 - >搜索目录 - >链接器

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

Settings->Compiler->Linker Settings->Link Libraries : 设置 - >编译器 - >链接器设置 - >链接库

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 

Like I said, there are lots of questions around the web about this particular error, but I haven't found anything with a solution for my setup. 就像我说的那样,网上有很多关于这个特殊错误的问题,但我没有找到任何解决方案来解决我的问题。 I admit I'm a little rusty on linking libraries but I swear I've tried every possible variation I could think up without success. 我承认我在连接图书馆方面有点生疏,但我发誓我已经尝试了所有可能的变化,我可以想到没有成功。

Here is the code: 这是代码:

/* 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;
}

I was facing the same error because of incorrect linker,and maybe your linkers are not correct. 由于链接器不正确,我遇到了同样的错误,也许你的链接器不正确。 Use these linkers 使用这些连接器

-lmysqlpp -lmysqlclient

Also you can follow this tutorial written by me on Eclipse (Ubuntu). 您也可以按照我在Eclipse(Ubuntu)上编写的本教程 Hope it will work with codeblocks too. 希望它也适用于代码块。

Here are a few of things you can try: 以下是您可以尝试的一些事项:

1. Try giving the linker an explicit name for your libs: 1.尝试为链接器提供一个显式名称:

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

Note: I added lib in front of those library names 注意:我在这些库名称前面添加了lib

Try linking in the dll for this instead of the static library if this doesn't work. 如果这不起作用,请尝试在dll中链接而不是静态库。

2. Try using MySQL_Driver instead of Driver(just for kicks): 2.尝试使用MySQL_Driver而不是Driver(仅用于踢):

#include <mysql_driver.h> 
...
...
sql::mysql::MySQL_Driver *driver;
...
...
try 
{     
  driver = sql::mysql::get_driver_instance();

3. You could try re-installing the connector c++ library from https://dev.mysql.com/downloads/connector/cpp/ 3.您可以尝试从 https://dev.mysql.com/downloads/connector/cpp/ 重新安装连接器c ++库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM