Wednesday, July 22, 2020

inserting and reading of tables in mysql and c++

hi to all, I am showing you how to connect c++ and mysql  and writing to and retrieving of table from mysql.

You must install rpm file "mysql-connector-c++-1.1.4-linux-glibc2.5-x86-64bit.rpm" to connect C++ client to MySQL server.
code is :

#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <mysql/mysql.h>

using namespace std;

int main()
{
    //Here we are creating MYSQL object conn and initializing it with nullptr
    MYSQL *conn = nullptr;
    MYSQL_ROW row = NULL; // MYSQL row object
    MYSQL_RES *resultSet = nullptr; // MYSQL Resultset

    int qstate; // query status

//    Allocates or initializes a MYSQL object suitable for mysql_real_connect(). If conn is a NULL pointer,
//    the function allocates, initializes, and returns a new object. Otherwise, the object is initialized and the
//    address of the object is returned. If mysql_init() allocates a new object, it is freed when mysql_close() is called
//    to close the connection.

    conn = mysql_init(nullptr);
    if(conn)
    {
        cout << "Connection succeeded" << endl;
    }
    else
    {
        cout << " Connection failed" << mysql_error(conn) << endl;
    }
//    mysql_real_connect() attempts to establish a connection to a MySQL server running on host. Client programs must
//    successfully connect to a server before executing any other API functions that require a valid MYSQL connection handler structure.
    conn = mysql_real_connect(conn, "serverora.db.net","rahul", "rahul", "cbs", 3306,NULL,0);
    if(conn) // successfully connected
    {
        string prdname;

        int stck = 0;
        int rate = 0;
        int prdno = 0;
        cout << " Product name : ";

        getline(cin,prdname);
        cout << " current Stock : ";
        cin  >> stck;
        cout << " Rate : ";
        cin  >> rate;
        cout << " Product ID : " ;
        cin  >> prdno;

//      converting int to string

        string str1 = to_string(stck);
        string str2 = to_string(rate);
        string str3 = to_string(prdno);
//    our sql statement will be :-

        string sql = "insert into tableProductRecords(ProductName, Stock, Rate, ProductID) values('"
                      +prdname + "','"+ str1 +"','"+ str2 +"','"+ str3 +"');";

        qstate = mysql_query(conn, sql.c_str());// sql.c_str() converts string object to standard string
        if(!qstate) // if qstate is zero that is successfully executed
        {
            cout << " record updated";
        }
        else
        {
            cout << " error : " << mysql_error(conn) << endl;
        }



        sql = "select *from tableProductRecords"; // SQL to retrieve table contents

        qstate = mysql_query(conn, sql.c_str()); // executing query
        if(!qstate)
        {
            resultSet = mysql_store_result(conn); // taking result set from mysql server

            while(row = mysql_fetch_row(resultSet) ) // fetching row one by one
            {
                //displaying table rows one by one
                cout << " Product name : "<< row[0] << "  "  << " Stock : " << row[1] << " Rate : " << row[2] << " Product ID : " << row[3] << endl;
            }
        }
        else
        {
            cout << "QUERY NOT EXECUTED : " << mysql_error(conn);
        }

    }

    mysql_close(conn);

    return 0;
}

No comments:

Post a Comment