Author Topic: mysql problem  (Read 533 times)

Offline Michiel

  • Single posting newcomer
  • *
  • Posts: 3
mysql problem
« on: October 03, 2018, 03:51:42 pm »
Hello, I'm trying to get Mysql working with codeblocks. I've linked the libraries (libmysql.lib an d mysqlclient.lib) an set the search directory to the mysql include folder.

It is actually linking the libraries (if I change the name I get an error), but when compiling I get errors about undefined references to mysql_init, mysql_query etc.

This is the code:
Code: [Select]
#include <iostream>

#include "mysql.h"

using namespace std;

MYSQL_RES* Perform_Query(MYSQL* connection, const char* query)
{
    // send the query to the database
   if (mysql_query(connection, query))
   {
      cout << "MySQL query error : %s\n" << mysql_error(connection) << endl;
      exit(1);
   }

    return mysql_use_result(connection);
}

int main()
{
    string mainServer="127.0.0.1";
    string mainDbUser="root";
    string mainDbPass="warmoes";
    MYSQL *connect; //database connection variable

    connect=mysql_init(NULL);
    if(!connect)
        cout<<"Couldn't initiate connector\n";

    if (mysql_real_connect(connect, mainServer.c_str(), mainDbUser.c_str(), mainDbPass.c_str(), "test" ,0,NULL,0))
    {
        cout<<"Connection done\n";
        MYSQL_RES* res = Perform_Query(connect, "show tables");
        cout << "Tables in database test" << endl;
        MYSQL_ROW row;
        while ((row = mysql_fetch_row(res)) != NULL)
        {
            cout << row[0] << endl;
        }
        mysql_free_result(res);

    }
    else
    {
        cout<<mysql_error(connect)<<endl;
    }
    mysql_close (connect);
    return 0;
}

Any clues on what's going wrong here? I work on Windows 7.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6489
    • My Best Post
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Stretch, compiling CB Trunk against wxWidgets 3.0.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Michiel

  • Single posting newcomer
  • *
  • Posts: 3
Re: mysql problem
« Reply #2 on: October 04, 2018, 11:03:46 am »
Hope this clarifies something...

-------------- Clean: Debug in test20181002db1 (compiler: GNU GCC Compiler)---------------

Cleaned "test20181002db1 - Debug"

-------------- Build: Debug in test20181002db1 (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -g -I"C:\Program Files\MySQL\MySQL Server 8.0\include" -I"C:\Program Files\MySQL\MySQL Server 8.0\include" -IC:\wxWidgets-3.0.4\include -I"C:\Program Files\MySQL\MySQL Server 8.0\include" -c D:\Michiel\Documents\C++\test20181002db1\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -L"C:\Program Files\MySQL\MySQL Server 8.0\include" -LC:\wxWidgets-3.1.1\lib\ -L"C:\Program Files\MySQL\MySQL Server 8.0\include" -o bin\Debug\test20181002db1.exe obj\Debug\main.o   "C:\Program Files\MySQL\MySQL Server 8.0\lib\libmysql.lib" "C:\Program Files\MySQL\MySQL Server 8.0\lib\mysqlclient.lib" "C:\Program Files\MySQL\MySQL Server 8.0\lib\libmysql.lib" "C:\Program Files\MySQL\MySQL Server 8.0\lib\mysqlclient.lib" "C:\Program Files\MySQL\MySQL Server 8.0\lib\libmysql.lib" "C:\Program Files\MySQL\MySQL Server 8.0\lib\mysqlclient.lib"
obj\Debug\main.o: In function `Z13Perform_QueryP5MYSQLPKc':
D:/Michiel/Documents/C++/test20181002db1/main.cpp:10: undefined reference to `mysql_query@8'
D:/Michiel/Documents/C++/test20181002db1/main.cpp:12: undefined reference to `mysql_error@4'
D:/Michiel/Documents/C++/test20181002db1/main.cpp:16: undefined reference to `mysql_use_result@4'
obj\Debug\main.o: In function `main':
D:/Michiel/Documents/C++/test20181002db1/main.cpp:26: undefined reference to `mysql_init@4'
D:/Michiel/Documents/C++/test20181002db1/main.cpp:30: undefined reference to `mysql_real_connect@32'
D:/Michiel/Documents/C++/test20181002db1/main.cpp:36: undefined reference to `mysql_fetch_row@4'
D:/Michiel/Documents/C++/test20181002db1/main.cpp:40: undefined reference to `mysql_free_result@4'
D:/Michiel/Documents/C++/test20181002db1/main.cpp:45: undefined reference to `mysql_error@4'
D:/Michiel/Documents/C++/test20181002db1/main.cpp:47: undefined reference to `mysql_close@4'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 2 second(s))
10 error(s), 0 warning(s) (0 minute(s), 2 second(s))
 

Offline sodev

  • Almost regular
  • **
  • Posts: 242
Re: mysql problem
« Reply #3 on: October 04, 2018, 01:25:02 pm »
These MySQL libraries, you didn't compile them yourself, did you? Because they look like they have been made with MSVC and you try to link them with GCC. Although they are only in C this usually does not work.

Best solution would be to compile MySQL (or at least only the client library) with your GCC, or compile your application with MSVC (but it better has to be the same MSVC compiler version or you will need two different runtimes at runtime).

Offline Michiel

  • Single posting newcomer
  • *
  • Posts: 3
Re: mysql problem
« Reply #4 on: October 16, 2018, 10:20:29 pm »
Thanks, I will give it a try!