Thursday, March 16, 2017

Connecting Code Blocks C++ with MySQL and Xampp

Connecting Code Blocks C++ with MySQL and Xampp

Requirements :

1. Code Blocks with mingw. I used version 16.01. You can download it from this link : http://sourceforge.net/projects/codeblocks/files/Binaries/16.01/Windows/codeblocks-16.01mingw-setup.exe

2. Download Xampp from their website. https://www.apachefriends.org/xampp-files/7.1.1/xampp-win32-7.1.1-0-VC14-installer.exe. I used the 7.1.1/PHP 7.1.1 (32 bit) version.

3. From https://dev.mysql.com/downloads/installer/, download Windows (x86, 32-bit), MSI Installer (version 5.7.17 , 1.7MB).

Note :
We need Xampp because it gives support of MySQL server, you can manage your database with it using integrated PHPmyAdmin.
Again we need MySQL Installer to install necessary Header and Lib files to use MySQL on Code Blocks.

Steps:
1. Install Code Blocks and Xampp first. Keep Xampp on its usual path (C:\xampp).

2. Open Xampp Control Panel and click Start button for Apache and MySQL modules, wait until they start. To check if Xampp is working correctly, from your Web Browser, go to http://localhost/dashboard/ to see if Welcome text comes or not, it you see Welcome to XAMPP then its fine.

3. Next run MySQL Installer (mysql-installer-web-community-5.7.17.0.msi), and in installation process choose Custom, then add MySQL Server 5.7.17 - X86 ( not all of its components, only check the Development Components and its Client C API library ). Also add Connector/C++ 1.1.8 - X86.

4. After completing download and installation, you would be asked to write Port number in which MySQL will run. You could see the number 3306. Change it to some other number, such as 3307 (as MySQL from Xampp runs in 3306 by default).

5. On the last step here, you would see some list of procedures and Execute button, click on execute, after completing ( or getting failed to start Server ), close and finish the setup.

6. Now run Code Blocks, start a new Console Application of C++.

7. Goto Build Options -> Linker Settings tab. Add -

C:\Program Files (x86)\MySQL\MySQL Server 5.7\lib\libmysql.lib
C:\Program Files (x86)\MySQL\MySQL Server 5.7\lib\mysqlclient.lib

(Click No if asked for Relative Path, change the path as required if you installed the MySQL Server in some different place that mentioned above.)

8. Goto Build Options -> Search directories tab. Add these folder -

C:\Program Files (x86)\MySQL\MySQL Server 5.7\include

9. Now with Explorer, goto "C:\Program Files (x86)\MySQL\MySQL Server 5.7\lib", and look for the file
"libmysql.dll". Copy it and paste it in these locations :

      1. C:\Windows\System32
      2. <your code blocks project path> where you can see *.cpp and *.cbp file
      3. <your code blocks project path>\bin\debug\

10. After doing all these things, write this code on your cpp file of your Project inside Code Blocks:



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


11. It should show the name of Tables inside the database "test" of MySQL. "test" is a pre-made database that comes with installation. It has no tables on the 1st time. So you need to create one atleast. To do that. goto
http://localhost/phpmyadmin/

12. Click "Databases" tab. Then click "test" from the list. On bottom panel (named Create Table), on the "Name" text box , write a table name, change number of columns to 1 (for testing purpose ofcourse) and click "go"

13. Then on the box under the Column "Name", write something for the name of the column, and click "Save" on the right bottom corner of the page.

14. Now build and run the code, and check if it shows something like
Connection done
Tables in database test
........

I hope that you would not get any error. Note that I am using Windows 8.1 64 Bit, and but I used programs of 32 bits.


13 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. i have never been grateful to anyone as i am to you guys thanks guys helped alot

    ReplyDelete
  3. Sir i want to use windows 10 for same purpose
    can i do that??????????????????????????

    ReplyDelete
  4. i have done this but it is showing 0xc00000ba error. can anyone help me out with this?

    ReplyDelete
  5. Hi, Nice article. Thank you for the article. Please see my article about Download XAMPP for Windows.

    ReplyDelete
  6. You have made some really good points there. I checked on the net to find out more about the issue and found most individuals will go along with your views on this web site.

    ReplyDelete
  7. Thankyou very much.
    Very Good Article...
    It helped a lot.

    ReplyDelete
  8. hello , i have an error in code blocks after writing the code
    :undefined reference to "mysql_query"

    ReplyDelete
    Replies
    1. same :(
      Do you know how should I solve that ?

      Delete
  9. Harrah's Cherokee Casino & Hotel - Mapyro
    Find your 동해 출장안마 way around the casino, find where everything is located, and see what's 구리 출장안마 great around Harrah's 문경 출장마사지 Cherokee Casino 청주 출장안마 & Hotel. 광양 출장마사지

    ReplyDelete