1

我想使用 Visual c++ 和 Visual Studio 2010 建立一个 mysql 连接。

我已经看到这个问题被问了几次,但我似乎无法弄清楚。

为什么没有任何真正有用且简单的分步教程呢?然后我的意思是:正确的下载链接,保存文件的位置。要添加到项目中的文件和要编写的正确代码。

我尝试使用许多不同的 c++ 连接器,甚至是完整的包。但没有成功。我在属性中将“include”文件夹添加到“C++ Additional Include Directories”中,将包含“libmysql.lib”的“lib”文件夹添加到“Linker Additional Library Directories”中,并将“libmysql.lib”添加到“链接器输入附加依赖项”。

我尝试使用如下代码:

#include <iostream>
#include <fstream>
#include <vector>
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
#include "my_global.h" // Include this file first to avoid problems
#include "mysql.h" // MySQL Include File
#include "XMLFile.h"
using namespace std;
using namespace rapidxml;

int main () {

    MYSQL *mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;

    string server = "localhost";
    string username = "root";
    string password = "";
    string database = "test";
    int port = 3306;

    mysql = mysql_init(0);
    if ( !mysql_real_connect( mysql, server.c_str(), username.c_str(), password.c_str(), database.c_str(), port, NULL, 0 ) ) {
        fprintf( stderr, "%s\n", mysql_error( mysql ) );
        return 0;
    }

    //further code should follow but i already get errors

    cin.get();
    return(0);
}

但我已经收到如下错误:

错误 4 错误 LNK2019: 函数 _main 中引用的未解析的外部符号 _mysql_error@4

4

3 回答 3

0

我不得不在 VS 2008 和 VS 2010 之间进行更改。
很多名称更改。以下是一些变化:

    #if (_MSC_VER == 1600)
    #include "driver/mysql_public_iface.h"
    #else
    #include "mysql_driver.h"
    #endif


namespace sql
{
    class Connection;
#if (_MSC_VER == 1600)
    class Driver;
#endif
    namespace mysql
    {
#if (_MSC_VER != 1600)
        class MySQL_Driver;
#endif      
    }
}


#if (_MSC_VER == 1600)
    mutable sql::Driver *                       m_p_sql_driver;
#else   
    mutable sql::mysql::MySQL_Driver *            m_p_sql_driver;
#endif


    void
    initialize_db_driver() const
    {
        static bool     driver_initialized = false;
        if (!driver_initialized)
        {
            wxMutexLocker   lock(sql_driver_mutex);
            if (!driver_initialized)
            {
                // There is something weird on the Windows 7 -64 bit platform
                //  with Visual Studio 2010 that causes the error message:
                //  "The application was unable to start correctly (0xc000007b)
                //  This is an experiment to see if the MySQL connector is the culprit.
    //          m_p_sql_driver = 0;

                //  When the MySQL Connector C++ is not used, the application starts up
                //      without the error.
    #if (_MSC_VER == 1600)
                m_p_sql_driver = 0;
                sql::Driver * p_driver = sql::mysql::get_driver_instance();
                m_p_sql_driver = p_driver;
    //          m_p_sql_driver = sql::mysql::get_driver_instance();
    #else
                m_p_sql_driver = sql::mysql::get_mysql_driver_instance();
    #endif
                if (m_p_sql_driver)
                {
                    driver_initialized = true;
                }
            }
        }
        return;
    }
于 2012-05-15T19:21:14.820 回答
0

您的 VC++ 似乎没有包含libmysql.lib在链接过程中,请确保其中Linker -> Command Line有类似/LIBPATH:"C:\mysql\path\to\mysql\lib" "libmysql.lib". 此外,您可以进行一些解决方法,例如
1. in Linker Input Additional Dependancies: 重命名libmysql.lib为其他不存在的名称,重建,验证是否存在链接错误LINK : fatal error LNK1104: cannot open file 'libmysql.lib'。这确保link确实找到了一个库libmysql.lib
2. 撤消步骤 1 中的更改,尝试在 .libmysql.lib 中添加路径VC++ Directories -> Library Directories

希望有所帮助。

于 2012-05-16T02:06:20.457 回答
0

这是正确的

您必须将'libmysql.lib'添加到链接器输入附加依赖项中:项目属性>链接器>输入>附加依赖项。然后将 libmysql.dll 复制到 exe 文件夹(PATH 中定义的文件夹)中,例如 'c:\windows'

你的问题将得到解决。

您可以在安装 mysql 的文件夹中找到 libmysql.dll 。

上面提到的使用“mysql.h”标头的问题。如果您想使用连接器++ 标头,那将有所不同。

于 2013-03-15T03:11:15.043 回答