Thursday, July 23, 2020

how to connect mysql and Qt and building QMSQL driver & make them loaded.

how to connect mysql and Qt and building QMSQL driver & make them loaded.

 hello all of you.

first install Qt( i installed in location /opt/Qt and choose "select all" ). now install mysql as folllows:-
1.  download mysql from official website. ( search "indexof mysql " in google).
2. install all rpms, you may be use systems configured as in client server model and so Qt must be in MySQL client.
3. run this command :- ( I am doing this in centos 6.6 VM)
    # service mysql start <--| (press enter) ( this is required for insertting  password for root)
4. then open file from location  given in "/root/.mysql_secret" file in root user folder.
that file contains password for root user of mysql database.
4.  set database as given step by step.
5. install mysql workbench.
6. make some users - database - tables - insert rows.
now this setups mysql server.

on client install all mysql rpms except mysql-server.xx.xx.rpm

7. now install in client mysql workbench (same as in server).
8. get connected in user  say "rahul" and password "rahul" host "oracle"

# mysql -u rahul -h oracle -p <--|
# password : <--| enter password for user rahul.
mysql >
if you get this prompt then you are successfully setuped client and server mysql.

now after installing Qt in  "/opt/Qt"
give this command for compilling  mysql plugin from qt source code:-
1. # cd /opt/Qt/5.3/Src/qtbase/src/plugins/sqldrivers/mysql <--|
2. # /opt/Qt/5.3/gcc_64/bin/qmake "INCLUDEPATH+=/usr/include" "LIBS+=-L/usr/lib64/mysql -lrt -ldl -lmysqlclient_r -lz -lcrypt -lnsl -lm -lssl -lcrypto" mysql.pro

3 # make
4. # make install
this will install plugin in appropriate location.
then check that plugin in following code :-
----------------------------------------------------
#include <QCoreApplication>
#include <QtSql>
#include <QtDebug>
#include <QSqlDatabase>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("oracle");
    db.setDatabaseName("music");
    //db.setPort(3306);
    db.setUserName("rahul");
    db.setPassword("rahul");
    if(db.open())
        qDebug() << " database connection open";
    else
        qDebug() << "cannot open database";



    return a.exec();
}

----------------------------------------------------
be sure add  "sql" tag in " project_file_name.pro" file as :-
QT +=  core sql
-----------------------------------------------------


No comments:

Post a Comment