Results 1 to 6 of 6

Thread: How to use QSQLITE module with Qt program?

  1. #1
    Join Date
    Oct 2011
    Posts
    13
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to use QSQLITE module with Qt program?

    Hi,
    I'm writing an application that should connect to a database. First, I've tried to use mySQL database but I had this error.

    Démarrage de C:\Users\HP\Desktop\lastClient\callTracker-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\debug\callTracker.exe... QSqlDatabase: QMYSQL driver not loaded
    QSqlDatabase: available drivers: QSQLITE

    So, now I'm trying with SQlite. I dowloaded the SQlite prebuild binaries and successfully created my database. But now, I don't know where to put the file calltracker.sqlite. And when I launch my application, there should be an error dialog box if application can not connect to the database. But whatever the server name I
    give, the connection seems to successful, but the application doesn't find any data in the database. I've made a lot of research on the net, and I'm a bit confused.
    So my questions are :

    -Does QtSDK already contain a SQLite database?
    -If so how can I access it?
    -Or is my code wrong? What should I change?


    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "FenetreAccueil.h"
    3.  
    4. int main(int argc,char* argv[]){
    5. QApplication app(argc, argv);
    6. FenetreAccueil login;
    7. login.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    FenetreAccueil.h

    Qt Code:
    1. #ifndef FENETREACCUEIL_H
    2. #define FENETREACCUEIL_H
    3.  
    4. #include <QtGui>
    5. #include <QtSql>
    6. #include "FenetrePrincipale.h"
    7.  
    8.  
    9. class FenetreAccueil : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. explicit FenetreAccueil(QWidget *parent = 0);
    15.  
    16. public slots:
    17. void connectDB();
    18.  
    19. private:
    20. QPushButton *m_connect;
    21. QPushButton *m_cancel;
    22. QLineEdit *m_password;
    23. QLineEdit *m_login;
    24. QLineEdit *m_addressBD;
    25. QLabel *m_label;
    26.  
    27. };
    28.  
    29. #endif // FENETREACCUEIL_H
    To copy to clipboard, switch view to plain text mode 

    FenetreAccueil.cpp
    Qt Code:
    1. #include "FenetreAccueil.h"
    2.  
    3. FenetreAccueil::FenetreAccueil(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. //Creation du layout de formulaire et ses widgets
    7. m_login=new QLineEdit;
    8. m_password=new QLineEdit;
    9. m_password->setEchoMode(QLineEdit::Password);//affichage d'asterisques a la saisie
    10. m_addressBD=new QLineEdit;//format adresse IP ou pas?
    11.  
    12. QFormLayout *formAccueil=new QFormLayout;
    13. formAccueil->addRow(tr("&Login"),m_login);
    14. formAccueil->addRow(tr("Password"),m_password);
    15. formAccueil->addRow(tr("mySQL server address"),m_addressBD);
    16.  
    17. //Creation d'un layout horizontal pour les boutons
    18. m_connect=new QPushButton(tr("Connect"));
    19. m_cancel=new QPushButton(tr("Cancel"));
    20.  
    21. QHBoxLayout *boutonLayout=new QHBoxLayout;
    22. boutonLayout->addWidget(m_connect);
    23. boutonLayout->addWidget(m_cancel);
    24.  
    25.  
    26. //Creation d'un layout pour le logo
    27. QLabel *logo=new QLabel(this);
    28. logo->setPixmap(QPixmap("images/grandlogo.png"));
    29.  
    30. QHBoxLayout *logoLayout=new QHBoxLayout;
    31. logoLayout->addWidget(logo);
    32.  
    33.  
    34. //Creation d'un layout pour le message d'acceuil
    35. m_label = new QLabel(tr("Welcome in callTracker"),this);
    36.  
    37. QHBoxLayout *labelLayout=new QHBoxLayout;
    38. labelLayout->addWidget(m_label);
    39.  
    40. //Creation du layout principal de la fenetre
    41. QVBoxLayout *layoutPrincipal=new QVBoxLayout;
    42.  
    43. //Ajout du layout d'accueil
    44. layoutPrincipal->addLayout(labelLayout);
    45.  
    46. //Ajout du layout de formulaire
    47. layoutPrincipal->addLayout(formAccueil);
    48.  
    49. //Ajout du layout des boutons
    50. layoutPrincipal->addLayout(boutonLayout);
    51.  
    52. setLayout(layoutPrincipal);
    53. setWindowTitle(tr("Welcome"));
    54. setWindowFlags(Qt::Tool);//Pour empecher le changement de dimensions de la fenetre
    55. resize(300,350);
    56.  
    57. //Generation des signaux et des slots
    58. connect(m_connect,SIGNAL(clicked()),this,SLOT(connectDB()));
    59. connect(m_cancel,SIGNAL(clicked()),qApp, SLOT(quit()));
    60. }
    61.  
    62. void FenetreAccueil::connectDB()
    63. {
    64. QString server;
    65. server=m_addressBD->text();
    66.  
    67. QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
    68. db.setHostName(server);
    69. db.setDatabaseName("calltracker");
    70. db.setUserName("appClient");
    71. db.setPassword("Clown2neige");
    72. bool ok=db.open();
    73.  
    74. if(ok)
    75. {
    76. FenetrePrincipale *fenP=new FenetrePrincipale;
    77. fenP->show();
    78. }
    79. else
    80. QMessageBox::critical(this,"callTracker",tr("Connection to database failed"));
    81.  
    82. }
    To copy to clipboard, switch view to plain text mode 


    Thanks

  2. #2
    Join Date
    Oct 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use QSQLITE module with Qt program?

    You miss qmysql driver.
    check http://doc.qt.nokia.com/4.7-snapshot/sql-driver.html or google it
    p.s. qsqlite is ready for use

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to use QSQLITE module with Qt program?

    With Sqlite the setDatabaseName() call should specify the full path and name of the existing Sqlite database (or ":memory:"). Sqlite will open() a file that does not exist... that's how you create a new database, which will be empty.

    If you want to use MySql then you need to build the plugin. There is plenty of information in the docs and this forum on doing that.

  4. #4
    Join Date
    Oct 2011
    Location
    India
    Posts
    13
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default Re: How to use QSQLITE module with Qt program?

    Which header files are necessary in order to work with Sqlite?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to use QSQLITE module with Qt program?

    The ones in the QtSql documentation, any one of the numerous SQL examples or even the example at the top of this thread.

  6. The following user says thank you to ChrisW67 for this useful post:

    pranj1488 (2nd November 2011)

  7. #6
    Join Date
    Jul 2009
    Location
    Italy, Pieve Ligure (GE)
    Posts
    55
    Thanks
    7
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use QSQLITE module with Qt program?

    Quote Originally Posted by phapha View Post
    [...]So, now I'm trying with SQlite. [...] I don't know where to put the file calltracker.sqlite.
    Assuming calltracker.sqlite is you DB file, you may put it whenever you want or like: the call to SetDatabaseName() will pass the file path.

    -Does QtSDK already contain a SQLite database?
    Not a database in itself, but all the code needed to create and access one (or many). In fact, you do not need the SQLite prebuilt binaries you downloaded for Qt to work with SQLite (of course, you may use them to access your DB independently of any Qt application).
    -If so how can I access it?
    From the code in your post, you only need to:
    *) replace "QMYSQL" with "QSQLITE" in the call to addDatabase(),
    *) instead of simply "calltracker", pass the complete path/fname.ext of DB file (either absolute or relative to your app working directory) in the call to db.setDatabaseName().

    AFAIK, SQLite does not provide any authentication, so the calls to db.setUserName() and db.setPassword() are useless (but also harmless) with SQLite.
    -Or is my code wrong? What should I change?
    Nothing is substantially wrong, it only need to be adapted to SQLite with the two above changes.

    Note that SQLite, when passed a DB file name which does not exists, creates one; so the call to open() rarely fails as a missing (or misplaced) DB file gets created by the call. Of course the file is created empty (no data in it!).

    M.

Similar Threads

  1. Need help on QSQLITE
    By lam-ang in forum Qt for Embedded and Mobile
    Replies: 15
    Last Post: 22nd June 2011, 11:07
  2. QSqlite issues
    By duave in forum Newbie
    Replies: 2
    Last Post: 3rd April 2011, 23:32
  3. QSqlite changes between 4.3 and 4.7
    By LKIM in forum Qt Programming
    Replies: 0
    Last Post: 22nd March 2011, 19:50
  4. QSqlite in QT4
    By sophister in forum Qt Programming
    Replies: 26
    Last Post: 5th April 2009, 12:52
  5. how to execute program that using qsqlite on different machine?
    By mismael85 in forum Installation and Deployment
    Replies: 2
    Last Post: 19th March 2008, 21:09

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.