Saturday, 17 November 2018


Hello friend today i am going to teach you, How to connect c++ program to database .
Let's start.



  • Step 1: To connect c++ program to database, we require database software like,MySQL, Oracle, XAMPP. Here i am using XAMPP Server, XAMPP is bascially database which is generally used for PHP Programming to develop dynamic website.

    • You Can Download XAMPP from Download XAMPP(Skip Step if you already have XAMPP)



  • Step 3: You require some header file 
  • Step 4:Open CodeBlock Go to File=>New=>Project
  • Choose Console Application and Click Go Button
  • Click Next
  • Choose C++ then click on Next Button
  • In Project Title: put your project name in my case i have entered DatabaseConnectivity
  • In Folder to create project in: Choose C:\ Drive of your computer
  • Project filename & Resulting filename will come automatically after giving Project Title &Folder to create project in
  • click Next
  • Here do nothing just lick on Finish Button
  • Now click on Source Folder then you can see main.cpp
  • Click on build & run to run project "You will see Hello World! means everything is correct till now"
  • Step 4:Now its time to add header file 
  • Go to C:\Program Files (x86)\CodeBlocks\MinGW\bin & paste the following header file into bin folder
  • Go to C:\Program Files (x86)\CodeBlocks\MinGW\include & paste the following header file into include folder
  • Go to C:\Program Files (x86)\CodeBlocks\MinGW\lib & paste the following header file into lib folder
  • Step 5:Now Come to your codeBlock righ click on your project and choose Build Option in my case i will right click in DatabaseConnectivity
  • After Click on Build Option you will see below screen
  • When asking for Relative Path Please "Select No"
  • Now Click on Search Directories
  • When asking for Relative Path Please "Select No"
  • Step 6:Open XAMPP 
  • Now Create mca database into XAMPP Control panel
  • and under mca Database create table with name of mcafy
  • the name the column as id ,name,phone
  • Now Put the below code into main.cpp 
  • #include <iostream>
    #include <windows.h>
    #include <mysql.h>

    using namespace std;

    int main()
    {
         MYSQL* conn;

         conn = mysql_init(0);

         conn = mysql_real_connect(conn,"localhost","root","","mca",0,NULL,0);

         if(conn)
            cout<<"connection to mca databse successful "<<endl;
         else
            cout<<"connection problem: "<<mysql_error(conn)<<endl;

        cout << "Hello world!" << endl;
        return 0;
    }
  • Step 7:  Below code to insert into table and retrieve from table
  • include <iostream>
    #include <string>
    #include <windows.h>
    #include <mysql.h>

    using namespace std;

    int main()
    {
        MYSQL* conn;
        MYSQL_ROW row;
        MYSQL_RES *res;
        int qstate;

           conn = mysql_init(0);
        if(conn)
            cout<<"connection object ok, conn="<<conn<<endl;
        else
            cout<<"conn object problem: "<<mysql_error(conn);

        conn = mysql_real_connect(conn,"localhost","root","","mca",0,NULL,0);

        if(conn)
        {
            cout<<"connected to database mca"<<endl;

            string id,name,phone;
            cout<<"enter id: "<<endl; cin>>id;
            cout<<"enter name: "<<endl; cin>>name;
            cout<<"enter phone: "<<endl; cin>>phone;

           
            string query="insert into mcafy(id,name,phone) values('"+id+"','"+name+"','"+phone+"')";


            const char* q = query.c_str();

            cout<<"query is: "<<q<<endl;
            qstate = mysql_query(conn,q);

            if(!qstate)
                cout<<"record inserted successfully..."<<endl;
            else
                cout<<"query problem: "<<mysql_error(conn)<<endl;

            qstate = mysql_query(conn,"select * from mcafy");

            if(!qstate)
            {
                res = mysql_store_result(conn);
                while(row=mysql_fetch_row(res))
                {
                    cout<<"id: "<<row[0]<< " "
                        <<"name: "<<row[1]<< " "
                        <<"phone: "<<row[2]<<endl;
                }
            }
            else
            {
                cout<<"query error: "<<mysql_error(conn)<<endl;
            }
        }
        else
        {
            cout<<"connection problem: "<<mysql_error(conn)<<endl;
        }

        mysql_close(conn);

        return 0;
    }


No comments:

Post a Comment