visual c++ 2010 mysql connect

依然范特西╮ 提交于 2019-12-11 08:38:51

问题


I want to make a mysql connection using visual c++ with visual studio 2010.

I have seen this question asked a couple of times, but I can't seem to figure it out.

Why aren't there any really useful and easy step by step tutorials for this? And then I mean like: the right download links, the places to save the files. The files to add to your project and the right code to write.

I tried using a lot of different c++ connectors or even the full packages. But without success. I added in the properties the "include" folder to "C++ Additional Include Directories", I added the "lib" folder which includes "libmysql.lib" to the "Linker Additional Library Directories" and I added "libmysql.lib" to the "Linker Input Additional Dependancies".

I tried using code like:

#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);
}

but i already get errors like:

Error 4 error LNK2019: unresolved external symbol _mysql_error@4 referenced in function _main


回答1:


I've had to make changes between VS 2008 and VS 2010.
A lot of name changes. Here are some of the changes:

    #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;
    }



回答2:


It's seem that your VC++ did not include libmysql.lib in link process, make sure that in Linker -> Command Line has something like /LIBPATH:"C:\mysql\path\to\mysql\lib" "libmysql.lib" . Moreover, You can make some workaround such as
1. in Linker Input Additional Dependancies: rename libmysql.lib to other name that not exist, rebuild, verify that there a link error LINK : fatal error LNK1104: cannot open file 'libmysql.lib'. This make sure link actually find a library libmysql.lib
2. Undo the change in step 1, try add path to libmysql.lib in VC++ Directories -> Library Directories.

Hope that help.




回答3:


that's right

u have to add 'libmysql.lib' to Linker Input Additional Dependancies in: project properties> linker> input> additional dependencies . then copy libmysql.dll in exe folder (the folder defined in PATH) such as 'c:\windows'

your problem will be solved.

you can find libmysql.dll in the folder you installed mysql .

what told above in for problem of using "mysql.h" header. if u want to use connector++ headers it would be different.



来源:https://stackoverflow.com/questions/10605672/visual-c-2010-mysql-connect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!