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
Code:
#include <QApplication>
#include "FenetreAccueil.h"
int main(int argc,char* argv[]){
FenetreAccueil login;
login.show();
return app.exec();
}
FenetreAccueil.h
Code:
#ifndef FENETREACCUEIL_H
#define FENETREACCUEIL_H
#include <QtGui>
#include <QtSql>
#include "FenetrePrincipale.h"
class FenetreAccueil
: public QWidget{
Q_OBJECT
public:
explicit FenetreAccueil
(QWidget *parent
= 0);
public slots:
void connectDB();
private:
};
#endif // FENETREACCUEIL_H
FenetreAccueil.cpp
Code:
#include "FenetreAccueil.h"
FenetreAccueil
::FenetreAccueil(QWidget *parent
) :{
//Creation du layout de formulaire et ses widgets
m_password
->setEchoMode
(QLineEdit::Password);
//affichage d'asterisques a la saisie m_addressBD
=new QLineEdit;
//format adresse IP ou pas?
QFormLayout *formAccueil=new QFormLayout;
formAccueil->addRow(tr("&Login"),m_login);
formAccueil->addRow(tr("Password"),m_password);
formAccueil->addRow(tr("mySQL server address"),m_addressBD);
//Creation d'un layout horizontal pour les boutons
boutonLayout->addWidget(m_connect);
boutonLayout->addWidget(m_cancel);
//Creation d'un layout pour le logo
logo
->setPixmap
(QPixmap("images/grandlogo.png"));
logoLayout->addWidget(logo);
//Creation d'un layout pour le message d'acceuil
m_label
= new QLabel(tr
("Welcome in callTracker"),
this);
labelLayout->addWidget(m_label);
//Creation du layout principal de la fenetre
//Ajout du layout d'accueil
layoutPrincipal->addLayout(labelLayout);
//Ajout du layout de formulaire
layoutPrincipal->addLayout(formAccueil);
//Ajout du layout des boutons
layoutPrincipal->addLayout(boutonLayout);
setLayout(layoutPrincipal);
setWindowTitle(tr("Welcome"));
setWindowFlags(Qt::Tool);//Pour empecher le changement de dimensions de la fenetre
resize(300,350);
//Generation des signaux et des slots
connect(m_connect,SIGNAL(clicked()),this,SLOT(connectDB()));
connect(m_cancel,SIGNAL(clicked()),qApp, SLOT(quit()));
}
void FenetreAccueil::connectDB()
{
server=m_addressBD->text();
db.setHostName(server);
db.setDatabaseName("calltracker");
db.setUserName("appClient");
db.setPassword("Clown2neige");
bool ok=db.open();
if(ok)
{
FenetrePrincipale *fenP=new FenetrePrincipale;
fenP->show();
}
else
QMessageBox::critical(this,
"callTracker",tr
("Connection to database failed"));
}
Thanks
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
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.
Re: How to use QSQLITE module with Qt program?
Which header files are necessary in order to work with Sqlite?
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.
Re: How to use QSQLITE module with Qt program?
Quote:
Originally Posted by
phapha
[...]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.
Quote:
-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).
Quote:
-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.
Quote:
-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.