Skip to main content

How to connect MYSQL using C++ in Linux

 Hello Everyone , So i was working on some project and there i've to connect MYSQL using C++ so i thought it will be great if i share that procedure to you.Then Let's begin....

So First of all you need mysql install in your Linux distro

sudo apt install mysql-client-core-8.0 && sudo apt-get install mysql-server

Now start mysql service and check status if it's active or not

sudo service mysql start && sudo service mysql status


Now create demosql.cpp file and include this lines

#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

using namespace std;

int main(){
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::PreparedStatement *pstmt;
sql::ResultSet *result;

// create driver instance
try{
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1","database-name","password");
        con->setSchema("database-name");
cout << "Connection made Successfully" << endl;
}
catch(sql::SQLException e){
cout << "Unable to connect Erro: " << e.what() << endl;
exit(1);
}
return 0;

}

if it works then congratulations you've successfully made connection with MYSQL

but if did not work then don't worry we'll fix issue 

first of all you need to compile above code with this command:

g++ demosql.cpp -o demosql -lmysqlcppconn

still didn't work? it means you don't have "mysqlcppcon" then install it and update repository with

sudo apt-get install libmysqlcppconn-dev && sudo apt update

now try compile with 

g++ demosql.cpp -o demosql -lmysqlcppconn

remember we'll compile always with above command ..and run with ./demosql

Now you might have thinking what's next ?

then let's create function to run query

I am gonna create only one function where we write query and that function will execute that query (you can create different function your self)

bool _execute(string query,sql::Connection *con,sql::Statement *stmt)
{
try{

stmt = con->createStatement();
stmt->execute(query);
cout << "TRUE" << endl;
return true;
}
catch(sql::SQLException e){
cout << "Unable to perform query Error: " << e.what() << endl;
cout << "FALSE" << endl;
exit(1);
return false;
}

}

Now you can use this function like belove,

_execute("CREATE TABLE demo1 (id serial PRIMARY KEY,
name VARCHAR(50), quantity INTEGER);",con,stmt);

you can change query according to your preferences..

Now how can you improve this.

Create Class which handles function (which you will create for executing various query) and there are lot of things you can do.

So that's all for today

Thank You 😊😊


Comments

Popular posts from this blog

Products Which I like

headphone Buy now Camera Buy now

Makefile

 You may have seen file named Makefile or you may have type command like make, make install. so what is this file Makefile and what are this command. let's discuss about it.

What is Docker?

 Hello Everyone , Today we'll talk about what is docker ?, how to use it ? and why you should use it ?, then let's begin...