6 Replies - 583 Views - Last Post: 03 July 2013 - 09:18 PM Rate Topic: -----

#1 illerhas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 03-July 13

C++ association with MySQL

Posted 03 July 2013 - 06:24 PM

Hello, I am relatively new to programming, and very new to C++. I have had some experience in using Perl, Python, and Visual Basic. I recently decided I wanted to try my hand at making a game, nothing too complex, just text based. I am very familiar with databases and I know that mixing code with a DB can be done. So, I wanted to be able to store information into a database: weapons, spells, items, character sheets, etc. and I found a tutorial by Mark Bain explaining the steps to take to allow the application to connect to MySQL. At the end he says I need to add this code to allow it to run properly "g++ -o db db.cc -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql", only problem is I use Windows 7, not linux. To write and compile the code i have been using Code::Blocks 12.11, and MySQL server is 5.6, with the C & C++ connectors. Thank You for taking the time to look it over and any replies on what I need to do to get my code running would be greatly appreciated.

#include <iostream>
#include <winsock.h>
#include <C:\Program Files\MySQL\MySQL Server 5.6\include\mysql.h>

using namespace std;
MYSQL *connection, mysql;
MYSQL_RES *result;
MYSQL_ROW row;
int query_state;
int main()
{
    //MySQL server is on the 3307 port, my database is named rpg.
    mysql_init(&mysql);
    connection = mysql_real_connect(&mysql,"localhost:3307","root","*secret*","rpg",0,0,0);
    if (connection == NULL) {cout << mysql_error(&mysql) << endl; return 1;}

    //weapons is the name of one of my tables in the rpg database
    query_state = mysql_query(connection, "select weapons()");
    if (query_state !=0) {cout << mysql_error(connection) << endl; return 1;
    }

    result = mysql_store_result(connection); while ( ( row = mysql_fetch_row(result)) != NULL ) { cout << row[0] << endl; }
}



The compiler i am using is GNU GCC Compiler
This is the error(s) I receive without the g++ -o db db.cc -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql in the "other Options" tab of the compiler setup.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ association with MySQL

#2 illerhas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 03-July 13

Re: C++ association with MySQL

Posted 03 July 2013 - 06:54 PM

Edit: i forgot to actually post my error.


obj\Debug\main.o||In function `main':|
D:\programming testing\main.cpp|13|undefined reference to `mysql_init@4'|
D:\programming testing\main.cpp|14|undefined reference to `mysql_real_connect@32'|
D:\programming testing\main.cpp|15|undefined reference to `mysql_error@4'|
D:\programming testing\main.cpp|18|undefined reference to `mysql_query@8'|
D:\programming testing\main.cpp|19|undefined reference to `mysql_error@4'|
D:\programming testing\main.cpp|22|undefined reference to `mysql_store_result@4'|
D:\programming testing\main.cpp|22|undefined reference to `mysql_fetch_row@4'|
||=== Build finished: 7 errors, 0 warnings (0 minutes, 0 seconds) ===|
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,491
  • Joined: 19-February 09

Re: C++ association with MySQL

Posted 03 July 2013 - 07:27 PM

Hi, you need to tell the compiler where the header files (.h) are, usually in some include directory. So you don't need to do this.

03	#include <C:\Program Files\MySQL\MySQL Server 5.6\include\mysql.h>



The errors are because the linker needs to be told where libraries (.lib on windows usually) are.
The directories for windows and linux will be different.

Steps 3 and 4.
Using libraries with Code::Blocks
Was This Post Helpful? 0
  • +
  • -

#4 illerhas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 03-July 13

Re: C++ association with MySQL

Posted 03 July 2013 - 08:04 PM

View Post#define, on 03 July 2013 - 07:27 PM, said:

Hi, you need to tell the compiler where the header files (.h) are, usually in some include directory. So you don't need to do this.

03	#include <C:\Program Files\MySQL\MySQL Server 5.6\include\mysql.h>



The errors are because the linker needs to be told where libraries (.lib on windows usually) are.
The directories for windows and linux will be different.

Steps 3 and 4.
Using libraries with Code::Blocks


Thank you for your quick reply. I added both the include file under the compiler tab under the search directories, so now I can just use the header without the path thank you. And under the linker tab, I added the path to get to the libs folder that contains both, libmysql.lib and mysqlclient.lib. just doing those steps i still get the same errors. I moved on to step 5 of the link you sent me at this point, and tried to add both .lib files to the projects build option's linker settings, and got the same error. after that I cleared that list and tried to add both .lib files to the compiler, and got the same error messages as before.
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,491
  • Joined: 19-February 09

Re: C++ association with MySQL

Posted 03 July 2013 - 08:40 PM

Something else to try, in the manual it says that if you are not including the path to enter the name without the lib prefix.

manual - 1.11.13 Including libraries
Was This Post Helpful? 0
  • +
  • -

#6 illerhas  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 03-July 13

Re: C++ association with MySQL

Posted 03 July 2013 - 09:15 PM

View Post#define, on 03 July 2013 - 08:40 PM, said:

Something else to try, in the manual it says that if you are not including the path to enter the name without the lib prefix.

manual - 1.11.13 Including libraries


Using just the file name, the errors didn't show up. So thank you for all the help!
Was This Post Helpful? 1
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1305
  • View blog
  • Posts: 4,491
  • Joined: 19-February 09

Re: C++ association with MySQL

Posted 03 July 2013 - 09:18 PM

MySQL.com - Compiling MySQL Clients on Microsoft Windows says.

Quote

The static library is named mysqlclient.lib and the dynamic library is named libmysql.dll. In addition, the libmysql.lib static import library is needed for using the dynamic library.



So either

1) use the static library mysqlclient.lib, which the above site says might give errors if not meeting some conditions.

or

2) use the dynamic library (libmysql.dll). With that link to the import library libmysql.lib and copy the dynamic library (libmysql.dll) to the same directory as your executable program/application.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1