Thursday, February 15, 2024

C++ and MySQL 5.6 program in client server model in CLI ( command line interface )

 CPP and MySQL 5.6 program :-
----------------------------

I presume you have already installed and configured centos 7 DNS server in one VM, centos 6 as MySQL server in second VM DNS Client and centos 7 as MySQL client as third DNS client VM having Code Blocks IDE for C++ command line interface. Also installed cpp and mysql connector rpm. Also you must have installed boost asio c++ library at least version 1.58 or above.

Also you have to install following rpm for mysql connection with CPP.

mysql-connector-c++-1.1.4-linux-glibc2.5-x86-64bit.rpm 

for "MySQL-5.6.23-1.linux_glibc2.5.x86_64.rpm-bundle.tar"

search "index of MySQL" in google and find web for rpms downloads.

To install boost asio library download it from "https://www.boost.org/users/download/". Now install like this :

Easy Build and Install of boost library :-
-----------------------------------------------------------

Issue the following commands in the shell (don't type #; that represents the shell's root prompt):
# cd path/to/boost_1_61_0

# ./bootstrap.sh --help

Select your configuration options and invoke ./bootstrap.sh again without the --help option. Unless you have write permission in your system's /usr/local/ directory, you'll probably want to at least use


# ./bootstrap.sh --prefix=path/to/installation/prefix

or just :-

# ./bootstrap.sh

to install somewhere else. Also, consider using the --show-libraries and --with-libraries=library-name-list options to limit the long wait you'll experience if you build everything. Finally,


# ./b2 install

will leave Boost binaries in the lib/ subdirectory of your installation prefix. You will also find a copy of the Boost headers in the include/ subdirectory of the installation prefix, so you can henceforth use that directory as an #include path in place of the Boost root directory.


Now set as follows in Codeblocks IDE :-
---------------------------------------

Create a new project -> console application -> next -> C++ ->
project title : CppMySQLConn
path : give as you want : "/opt/projects/cpp/cppmysqlconn"
next -> next

On IDE : proect-> build options :-


1. Compiler flags     : c++14 : right tick on it
                                : -m 64 : right tick on it

2. linker settings    : mysqlcppconn, pthread, dl

3. search directories     :
compiler     : /usr/local/boost/include/boost
            : /usr/include/mysql

    linker         : /usr/lib64/mysql
            : /usr/local/boost/lib


Now open project "CppConnMysql" in CodeBlocks C++ IDE and do as follows :-

/*
    here :     mysql server      : centos6mss.db.net

                    ip address    : 192.168.2.3
                   user name     : rahul
                    password     : rahul
                    port         : 3306
                    database schema    : cbs

Database cbs is :- ( you have to populate this table first )
------------------

As : 'tablename'.'column' :-
----------------------------

`tableBilling`.`id`,
`tableBilling`.`ProductID`,
`tableBilling`.`Quantity`,
`tableBilling`.`Dateofsale`,
`tableBilling`.`Total`,
`tableBilling`.`Billno`,
`tableBilling`.`CustomerID`,
`tableCustomers`.`id`,
`tableCustomers`.`CustomerName`,
`tableCustomers`.`ContactAddress`,
`tableCustomers`.`MobileNo`,
`tableCustomers`.`BillNo`,
`tableProductRecords`.`ProductName`,
`tableProductRecords`.`Stock`,
`tableProductRecords`.`Rate`,
`tableProductRecords`.`ProductID`

*/
// program is :-

--------------------------

*/
#include <cstdlib>
#include <iostream>

#include <mysql_connection.h>
#include <mysql_driver.h>

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

using namespace std;
//using namespace sql;
int main()
{
    cout << endl;

    cout << "Let's have a MySQL count from last to first .... " << endl;

    try
    {
        sql::Driver *driver;
        sql::Connection *conn;
        sql::Statement *stmt;
        sql::ResultSet *res;
        sql::PreparedStatement *pstmt;

        driver = get_driver_instance();
        conn = driver->connect("centos6mss.db.net:3306", "username", "password");
//        conn = driver->connect("192.168.2.3:3306", "username", "password");
        conn->setSchema("cbs");

        pstmt = conn->prepareStatement("select id from tableCustomers;");
        res = pstmt->executeQuery();
        res->afterLast();

        while(res->previous())
        {
            cout << "customers id : " << res->getInt("id") << endl;
        }
        delete res;
        delete pstmt;
        delete conn;
    }
    catch(sql::SQLException &e)
    {
        cout << "#ERR : sql excep : in " << __FILE__ << endl;
        cout << "#Func : in " << __FUNCTION__<< " on line : " << __LINE__ << endl;
        cout << "#ERR what : " << e.what() << endl;
    }

    cout << endl;

    return EXIT_SUCCESS;
}

/*
output  :-
----------

[rahul@client1 Debug]$ ./first

Let's have a MySQL count from last to first ....
customers id : 25
customers id : 24
customers id : 23
customers id : 22
customers id : 21
customers id : 20
customers id : 19
customers id : 18
customers id : 17
customers id : 16
customers id : 15
customers id : 14
customers id : 13
customers id : 12
customers id : 11
customers id : 10
customers id : 9
customers id : 8
customers id : 6
customers id : 5
customers id : 4
customers id : 3
customers id : 2

[rahul@client1 Debug]$
*/