MySQL with C++

Here’s an example for connecting C++ program with MySQL.

You can simply download code from Github [Link]

or

Do as following

Write following code in text editor and save file as mysql.cpp


// Include Header Files
#include <iostream>
#include <cstdio>
#include <cstdlib>

// For MySQL Connection
#include <mysql.h>

using namespace std;

// Defining Constant Variables
#define SERVER "localhost"
#define USER "root"
#define PASSWORD "password"
#define DATABASE "test"

int main()
{
    MYSQL *connect;
    connect = mysql_init(NULL);

    if (!connect)
    {
        cout << "Mysql Initialization Failed";
        return 1;
    }

    connect = mysql_real_connect(connect, SERVER, USER, PASSWORD, DATABASE, 0,NULL,0);

    if (connect)
    {
        cout << "Connection Succeeded\n";
    }
    else
    {
        cout << "Connection Failed\n";
    }

    MYSQL_RES *res_set;
    MYSQL_ROW row;

    // Replace MySQL query with your query

    mysql_query (connect,"show tables");

    unsigned int i=0;

    res_set=mysql_store_result(connect);

    unsigned int numrows = mysql_num_rows(res_set);
    
    cout << " Tables in " << DATABASE << " database " << endl;

    while (((row=mysql_fetch_row(res_set)) !=NULL))
    {
        cout << row[i] << endl;
    }

    mysql_close (connect);

    return 0;
}

Now compile and run this program in terminal using following statements.

$ g++ -o mysql mysql.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql

$ ./mysql

Comments

8 responses to “MySQL with C++”

  1. buy 7 keto Avatar

    It’s going to be ending of mine day, however before finish I am reading this wonderful piece of writing to increase my experience.

  2. buyer of structured settlement Avatar

    I got this web site from my friend who shared with me
    regarding this web page and at the moment
    this time I am browsing this website and reading very informative
    content at this time.

  3. online casino Avatar

    Heya i am for the first time here. I came across this board and I find It truly useful & it helped me out much.
    I hope to give something back and help others like you aided me.

  4. Elwood Avatar

    Just want to say your article is as astonishing.
    The clearness in your post is just cool and i can assume you’re an expert on this subject. Well with your permission let me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please keep up the enjoyable work.

    1. mandeepsimak Avatar

      Yes, you can save RSS feed.

  5. natural green coffee bean Avatar

    Hello friends, how is all, and what you desire to say on
    the topic of this article, in my view its actually remarkable designed for
    me.

  6. teeth whitening uk Avatar

    Greetings! Very helpful advice within this post!
    It’s the little changes that produce the largest changes. Many thanks for sharing!

  7. Cute embroidery Avatar

    Glad I found this on google.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.