locked
Errors in Database Connection SQL RRS feed

  • Question

  • Hi friends,

    I tried the below code to connect to the mysql database using c++. I've added and referenced all the required lib and include folders.

    #include "stdafx.h"

    #include<stdio.h>
    #include<conio.h>
    #include<windows.h>
    #include<mysql.h>

    static char *opt_host_name="localhost";
    static char *opt_user_name="root";
    static char *opt_password="root";
    static unsigned int opt_port_num=3306;
    static char *opt_socket_name=NULL;
    static char *opt_dbname="traffic_details";
    static unsigned int opt_flags=0;
    static MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW rows;
    int main(int argc,char *argv[])
    {
    conn=mysql_init(NULL);
    if(conn==NULL)
    {
    fprintf(stderr,"Something went wrong:failed!");
    exit(1);
    }
    if(( mysql_real_connect(conn, opt_host_name, opt_user_name, opt_password, opt_dbname ,opt_port_num, opt_socket_name, opt_flags ))==NULL)
    {
    fprintf(stderr,"%s",mysql_error(conn));
    mysql_close(conn);
    exit(1);
    }
    else
    {
    printf("\nSuccess!");
    }
    mysql_close(conn);
    printf("\nPress any key to continue");
    getch();
    return 0;
    }

    My Include folders.

    C:\Program Files\MySQL\Connector C++ 1.1.0\include\cppconn;

    C:\Program Files\MySQL\Connector C++ 1.1.0\include;

    C:\Program Files\MySQL\MySQL Server 5.5\include\mysql\psi;

    C:\Program Files\MySQL\MySQL Server 5.5\include\mysql;

    C:\Program Files\MySQL\MySQL Server 5.5\include;

    My lib folders

    C:\Program Files\MySQL\Connector C++ 1.1.0\lib\debug;

    C:\Program Files\MySQL\MySQL Server 5.5\lib\debug;

    C:\Program Files\MySQL\MySQL Server 5.5\lib;

    libmysql.dll and libmysql.lib also added to the project path.

    And in my linker inputs I added the following

    C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib;

    mysqlclient.lib;

    mysqlcppconn.lib;

    mysqlcppconn-static.lib;

    But I got the following errors.

    Error    5    error LNK1120: 4 unresolved externals    C:\Users\Krishnananthan\documents\visual studio 2010\Projects\TestProject\Debug\TestProject.exe    TestProject
    Error    1    error LNK2019: unresolved external symbol _mysql_close@4 referenced in function _main    C:\Users\Krishnananthan\documents\visual studio 2010\Projects\TestProject\TestProject\TestProject.obj    TestProject
    Error    2    error LNK2019: unresolved external symbol _mysql_error@4 referenced in function _main    C:\Users\Krishnananthan\documents\visual studio 2010\Projects\TestProject\TestProject\TestProject.obj    TestProject
    Error    4    error LNK2019: unresolved external symbol _mysql_init@4 referenced in function _main    C:\Users\Krishnananthan\documents\visual studio 2010\Projects\TestProject\TestProject\TestProject.obj    TestProject
    Error    3    error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function _main    C:\Users\Krishnananthan\documents\visual studio 2010\Projects\TestProject\TestProject\TestProject.obj    TestProject

    How to solve these errors? I've gone through several websites, but I couldn't be able to resolve these errors yet. Please help me on this.

    Thanks in advance.

    Sunday, June 16, 2013 11:00 AM

Answers

  • I tried the link also, but same errors. I couldn't be able to understand the problem. In the link you provided they told about the static and dynamic projects. I don't have much knowledge regarding that. All I want to do is connecting to a database and display the value. I already posted the values of Additional include directories,Additional dependencies and Additional library directories. Treat wchat_t as default built in type is ok. Please help me on this.

    You dont need to specify the path of each lib. Just specify libmysql.lib; mysqlclient.lib; mysqlcppconn.lib; mysqlcppconn-static.lib; in Additional dependencies. Also i think you specify the wrong path  for  libmysql.lib in Additional dependencies.

    C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib;


    Thanks, Renjith V R

    • Marked as answer by Elegentin Xie Wednesday, June 26, 2013 11:23 AM
    Tuesday, June 18, 2013 5:15 AM

All replies

  • I'm not a MySQL programmer, but I would tackle this by picking one of the unresolved references (say, mysql_init()) and finding out which DLL it is defined in. The utility dumpbin.exe is your friend. Once you know the DLL, recheck your settings to ensure that the corresponding LIB file has been specified. Typically this is defined under Linker, Input, Additional Dependencies.

    We don't get many MySQL questions in this forum, probably because there are better forums to find the answer. Try one of the discussion groups found here.

    Sunday, June 16, 2013 5:05 PM
  • Hi friends,

    I tried the below code to connect to the mysql database using c++. I've added and referenced all the required lib and include folders.

    #include "stdafx.h"

    #include<stdio.h>
    #include<conio.h>
    #include<windows.h>
    #include<mysql.h>

    static char *opt_host_name="localhost";
    static char *opt_user_name="root";
    static char *opt_password="root";
    static unsigned int opt_port_num=3306;
    static char *opt_socket_name=NULL;
    static char *opt_dbname="traffic_details";
    static unsigned int opt_flags=0;
    static MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW rows;
    int main(int argc,char *argv[])
    {
    conn=mysql_init(NULL);
    if(conn==NULL)
    {
    fprintf(stderr,"Something went wrong:failed!");
    exit(1);
    }
    if(( mysql_real_connect(conn, opt_host_name, opt_user_name, opt_password, opt_dbname ,opt_port_num, opt_socket_name, opt_flags ))==NULL)
    {
    fprintf(stderr,"%s",mysql_error(conn));
    mysql_close(conn);
    exit(1);
    }
    else
    {
    printf("\nSuccess!");
    }
    mysql_close(conn);
    printf("\nPress any key to continue");
    getch();
    return 0;
    }

    My Include folders.

    C:\Program Files\MySQL\Connector C++ 1.1.0\include\cppconn;

    C:\Program Files\MySQL\Connector C++ 1.1.0\include;

    C:\Program Files\MySQL\MySQL Server 5.5\include\mysql\psi;

    C:\Program Files\MySQL\MySQL Server 5.5\include\mysql;

    C:\Program Files\MySQL\MySQL Server 5.5\include;

    My lib folders

    C:\Program Files\MySQL\Connector C++ 1.1.0\lib\debug;

    C:\Program Files\MySQL\MySQL Server 5.5\lib\debug;

    C:\Program Files\MySQL\MySQL Server 5.5\lib;

    libmysql.dll and libmysql.lib also added to the project path.

    And in my linker inputs I added the following

    C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib;

    mysqlclient.lib;

    mysqlcppconn.lib;

    mysqlcppconn-static.lib;

    But I got the following errors.

    Error    5    error LNK1120: 4 unresolved externals    C:\Users\Krishnananthan\documents\visual studio 2010\Projects\TestProject\Debug\TestProject.exe    TestProject
    Error    1    error LNK2019: unresolved external symbol _mysql_close@4 referenced in function _main    C:\Users\Krishnananthan\documents\visual studio 2010\Projects\TestProject\TestProject\TestProject.obj    TestProject
    Error    2    error LNK2019: unresolved external symbol _mysql_error@4 referenced in function _main    C:\Users\Krishnananthan\documents\visual studio 2010\Projects\TestProject\TestProject\TestProject.obj    TestProject
    Error    4    error LNK2019: unresolved external symbol _mysql_init@4 referenced in function _main    C:\Users\Krishnananthan\documents\visual studio 2010\Projects\TestProject\TestProject\TestProject.obj    TestProject
    Error    3    error LNK2019: unresolved external symbol _mysql_real_connect@32 referenced in function _main    C:\Users\Krishnananthan\documents\visual studio 2010\Projects\TestProject\TestProject\TestProject.obj    TestProject

    How to solve these errors? I've gone through several websites, but I couldn't be able to resolve these errors yet. Please help me on this.

    Thanks in advance.

    Read below article. If it does not helps, post the values of Additional include directories,Additional dependencies and Additional library directories. Also check value of C++->Language->Treat wchat_t as default built in type

    Building MySQL Connector/C++ Windows Applications with Microsoft Visual Studio


    Thanks, Renjith V R


    Monday, June 17, 2013 2:37 PM
  • I tried the link also, but same errors. I couldn't be able to understand the problem. In the link you provided they told about the static and dynamic projects. I don't have much knowledge regarding that. All I want to do is connecting to a database and display the value. I already posted the values of Additional include directories,Additional dependencies and Additional library directories. Treat wchat_t as default built in type is ok. Please help me on this.
    Monday, June 17, 2013 6:18 PM
  • Please help me on this.
    Did you try dumpbin as I suggested? What did you discover?
    Monday, June 17, 2013 7:37 PM
  • I tried the link also, but same errors. I couldn't be able to understand the problem. In the link you provided they told about the static and dynamic projects. I don't have much knowledge regarding that. All I want to do is connecting to a database and display the value. I already posted the values of Additional include directories,Additional dependencies and Additional library directories. Treat wchat_t as default built in type is ok. Please help me on this.

    You dont need to specify the path of each lib. Just specify libmysql.lib; mysqlclient.lib; mysqlcppconn.lib; mysqlcppconn-static.lib; in Additional dependencies. Also i think you specify the wrong path  for  libmysql.lib in Additional dependencies.

    C:\Program Files\MySQL\MySQL Server 5.5\lib\libmysql.lib;


    Thanks, Renjith V R

    • Marked as answer by Elegentin Xie Wednesday, June 26, 2013 11:23 AM
    Tuesday, June 18, 2013 5:15 AM