Search This Blog

Sunday, June 1, 2014

Qt Creator & Mysql

   To demonstrate mysql connectivity with Qt ,Let us create a desktop application in qt creator that will add some record to mysql.Design your screen as follows



meanwhile on your database create a table as

              CREATE TABLE QT_TEST
             (
                PName VARCHAR(200),
                PCountry VARCHAR(100),
                PAddress VARCHAR(500)
             )
Lets comeback to our Qt App,In our QT App the Name control is of type lineEdit while Address control is of type TextEdit as it can be multiliner.

Here is main controls

1) txtlnName: Name of Person
2) txtteAddress: Address of Person
3) listWidget: Single select Drop down list
4) pbSubmit:submiting user input
5) pbReset: reset user screen

into Mainwindow.cpp add following header

#include <QtSql>

now on submit button's click slot,  read user input like below

    QString Name =ui->txtlnName->text();
    QString Address =ui->txtteAddress->toPlainText();

    //listbox
    QListWidgetItem *sel_item = ui->listWidget->currentItem();
    QString Country= sel_item->text();



Now call a function that will save user input to mysql



bool InsertRecord(QString Name,QString Country,QString Address)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");//Driver
    db.setHostName("localhost");//server
    db.setPort(3306);//port
    db.setDatabaseName("test");//database name
    db.setUserName("root");//user name
    db.setPassword("user_pwd");//password

    bool ok = db.open();

    QSqlQuery query;
    if (ok){
            query.prepare( "INSERT INTO QT_TEST (PName,PCountry,PAddress) VALUES ( :PName,:PCountry,:PAddress ) ");
            query.bindValue( ":PName", Name);
            query.bindValue( ":PCountry", Country);
            query.bindValue( ":PAddress", Address);QT += sql

            query.exec();
            db.close();
            return true;
        }
    return false;
}

Try Run your application and submit input.Now go to your mysql GUI or command prompt and run

            SELECT * FROM QT_TEST

You will see your record has been inserted into table.

Note: If you got error about  Qtsql header not found try adding  
                      QT += sql
           into project file yourprojectname.pro file.

GitHub : https://github.com/gitsangramdesai/qt-mysql-demo

Useful Linux Commands for User handling

1) How to find out under which user session you are working ?
         use "whoami" to find out user name of user currently active
                   >whoami

 2) How to add new user ?
                   >useradd your_user_name
                  rather
                   >sudo useradd your_user_name

 3) How to change password of user ?
                 >passwd your_user_name
                      rather
                >sudo passwd your_user_name

 4) Delete a user
               >userdel user_name_to_delete 
5) List all users on Linux Box
             >cat /etc/passwd

6) How to find out current Users & their process?
     'w' is command that we can use here
              >w

7) Currently logged in users:
       To list currently logged in users 'who' command can be used
             >who

8) How to see all groups ?
       To list out all groups on your machine
            >cat /etc/group

 Please use manpages for more details about each command like say 
          >whoami  -h

Thanks & Happy Weekend!