Results 1 to 4 of 4

Thread: How to connect MySql from Qt in CentOS

  1. #1
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Exclamation How to connect MySql from Qt in CentOS

    Hello Everyone,

    Well i am working on CentOS and trying to create a simple program which requires to connect with MySql.

    Qt : 5
    OS : CentOS

    Qt Code:
    1. #include <QApplication>
    2. #include <QTableWidget>
    3. #include <QMessageBox>
    4. #include <QtSql>
    5.  
    6. int main(int argc,char* argv[])
    7. {
    8. QApplication app(argc,argv);
    9. QTableWidget* table = new QTableWidget();
    10. table->setWindowTitle("Connect to Mysql Database Example");
    11.  
    12. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    13. db.setHostName("192.168.11.3");
    14. db.setDatabaseName("menudb");
    15. db.setUserName("root");
    16. db.setPassword("test");
    17. if (!db.open())
    18. {
    19. QMessageBox::critical(0, QObject::tr("Database Error"),
    20. db.lastError().text());
    21. }
    22.  
    23. QSqlQuery query("SELECT * FROM test");
    24.  
    25. table->setColumnCount(query.record().count());
    26. table->setRowCount(query.size());
    27.  
    28. int index=0;
    29. while (query.next())
    30. {
    31. table->setItem(index,0,new QTableWidgetItem(query.value(0).toString()));
    32. table->setItem(index,1,new QTableWidgetItem(query.value(1).toString()));
    33. index++;
    34. }
    35.  
    36. table->show();
    37. return app.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 

    Well the problem is MySql DRIVERS are not loaded.
    Which drivers should i use in CentOS to connect my program with MySql?

  2. #2
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How to connect MySql from Qt in CentOS

    If you have installed Qt with yum from a CentOS repos, check if you didn't forget to install mysql driver package too, database drivers are in individual packages. If you use your own compiled qt installation check if mysql driver has been selected during the configure.

  3. #3
    Join Date
    Feb 2013
    Location
    India
    Posts
    153
    Thanks
    27
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to connect MySql from Qt in CentOS

    Hei Nix,

    I have searched on google and found many drivers for mysql.
    Which one is suitable for Qt 5 and CentOS. ?


    THANKS

  4. #4
    Join Date
    Dec 2008
    Location
    France
    Posts
    93
    Thanked 23 Times in 22 Posts
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: How to connect MySql from Qt in CentOS

    If you are using Qt version installed by your distrib package manager, just do :
    Qt Code:
    1. yum search qt | grep -i mysql
    To copy to clipboard, switch view to plain text mode 
    It should return a few lines, in my REL the line you are looking for is :
    Qt Code:
    1. qt4-mysql.x86_64 : MySQL drivers for Qt's SQL classes
    To copy to clipboard, switch view to plain text mode 
    But the package name change from one distrib to an onother, you have to find yours in the list.
    Install the package :
    Qt Code:
    1. yum install qt4-mysql
    To copy to clipboard, switch view to plain text mode 
    Then everything should works.

    If you are using your own built Qt lib, check during the configure if the plugin for mysql is selected, if not check the package mysql is installed with yum (it's the client for mysql), and redo configure.

  5. The following user says thank you to nix for this useful post:

    karankumar1609 (29th June 2013)

Similar Threads

  1. cannot connect to mysql in QT 4.7.2
    By vahidsamimi in forum Installation and Deployment
    Replies: 3
    Last Post: 5th September 2012, 14:55
  2. Replies: 13
    Last Post: 17th June 2011, 18:19
  3. How to connect Qt with MYSQL??
    By Gokulnathvc in forum Newbie
    Replies: 10
    Last Post: 24th March 2011, 01:52
  4. Qt connect different MySQL
    By weixj2003ld in forum Qt Programming
    Replies: 0
    Last Post: 5th August 2009, 09:35
  5. How to connect MySQL with QT
    By diego in forum Qt Programming
    Replies: 0
    Last Post: 27th May 2009, 06:34

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.