integrating MySQL with Visual C++

This is a very old but still useful tutorial on integrating MySQL with C++

This is a working code example of using C++ to interface with MySQL.  I have tested this code to work several times, please do note however that it was originally written in Visual Studio 2005, at this point in time Visual Studio 2010, and MySQL 5.1 is out, however with some minor tweaking you should be able to receive the same results.  The source code itself is documented very well and should explain everything you need to know on the coding side, however setting up the project may be a little different to what you are use to, as you need to include the mysql library to your project. Here is the Visual Studio 2005 example on how to do that.

First things first!

Open Visual Studio, or whatever C++ Programming IDE that you normally use. 1. Create a new Empty Console Project/Solution. – That means it has nothing in it no pre defined headers nothing!! 2. Add a new source file to your project. Lets just call it main.cpp 3. This step is VERY IMPORTANT. If you don’t follow this you will get unresolved external errors when you try to link your app. – Open your project settings and go to linker. – Add additional dependency : libmysql.lib 4. Make sure you have the include files, libraries, and binary files. If you don’t have it then just download MySQL Server without the installer, then point the include paths for your project to the lib directories for mysql that you downloaded. I have included mine in global settings, because I use it so much and it’s too tedious in my opinion to constantly re-add it for every project.

#include "my_global.h" // Include this file first to avoid problems
#include "mysql.h" // MySQL Include File
#define SERVER "localhost"
#define USER "username"
#define PASSWORD "password"
#define DATABASE "databasename"

int main()
    MYSQL *connect; // Create a pointer to the MySQL instance
    connect=mysql_init(NULL); // Initialise the instance
    /* This If is irrelevant and you don't need to show it. I kept it in for Fault Testing.*/
    if(!connect)    /* If instance didn't initialize say so and exit with fault.*/
        fprintf(stderr,"MySQL Initialization Failed");
        return 1;
    /* Now we will actually connect to the specific database.*/

    /* Following if statements are unneeded too, but it's worth it to show on your
    first app, so that if your database is empty or the query didn't return anything it
    will at least let you know that the connection to the mysql server was established. */

        printf("Connection Succeeded\n");
        printf("Connection Failed!\n");
    MYSQL_RES *res_set; /* Create a pointer to recieve the return value.*/
    MYSQL_ROW row;  /* Assign variable for rows. */
    mysql_query(connect,"SELECT * FROM TABLE");
    /* Send a query to the database. */
    unsigned int i = 0; /* Create a counter for the rows */

    res_set = mysql_store_result(connect); /* Receive the result and store it in res_set */

    unsigned int numrows = mysql_num_rows(res_set); /* Create the count to print all rows */

    /* This while is to print all rows and not just the first row found, */

    while ((row = mysql_fetch_row(res_set)) != NULL){
        printf("%s\n",row[i] != NULL ?
        row[i] : "NULL"); /* Print the row data */
    mysql_close(connect);   /* Close and shutdown */
    return 0;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s