phapha
28th October 2011, 18:45
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
#include <QApplication>
#include "FenetreAccueil.h"
int main(int argc,char* argv[]){
QApplication app(argc, argv);
FenetreAccueil login;
login.show();
return app.exec();
}
FenetreAccueil.h
#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:
QPushButton *m_connect;
QPushButton *m_cancel;
QLineEdit *m_password;
QLineEdit *m_login;
QLineEdit *m_addressBD;
QLabel *m_label;
};
#endif // FENETREACCUEIL_H
FenetreAccueil.cpp
#include "FenetreAccueil.h"
FenetreAccueil::FenetreAccueil(QWidget *parent) :
QWidget(parent)
{
//Creation du layout de formulaire et ses widgets
m_login=new QLineEdit;
m_password=new QLineEdit;
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
m_connect=new QPushButton(tr("Connect"));
m_cancel=new QPushButton(tr("Cancel"));
QHBoxLayout *boutonLayout=new QHBoxLayout;
boutonLayout->addWidget(m_connect);
boutonLayout->addWidget(m_cancel);
//Creation d'un layout pour le logo
QLabel *logo=new QLabel(this);
logo->setPixmap(QPixmap("images/grandlogo.png"));
QHBoxLayout *logoLayout=new QHBoxLayout;
logoLayout->addWidget(logo);
//Creation d'un layout pour le message d'acceuil
m_label = new QLabel(tr("Welcome in callTracker"),this);
QHBoxLayout *labelLayout=new QHBoxLayout;
labelLayout->addWidget(m_label);
//Creation du layout principal de la fenetre
QVBoxLayout *layoutPrincipal=new QVBoxLayout;
//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(conn ectDB()));
connect(m_cancel,SIGNAL(clicked()),qApp, SLOT(quit()));
}
void FenetreAccueil::connectDB()
{
QString server;
server=m_addressBD->text();
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
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
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
#include <QApplication>
#include "FenetreAccueil.h"
int main(int argc,char* argv[]){
QApplication app(argc, argv);
FenetreAccueil login;
login.show();
return app.exec();
}
FenetreAccueil.h
#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:
QPushButton *m_connect;
QPushButton *m_cancel;
QLineEdit *m_password;
QLineEdit *m_login;
QLineEdit *m_addressBD;
QLabel *m_label;
};
#endif // FENETREACCUEIL_H
FenetreAccueil.cpp
#include "FenetreAccueil.h"
FenetreAccueil::FenetreAccueil(QWidget *parent) :
QWidget(parent)
{
//Creation du layout de formulaire et ses widgets
m_login=new QLineEdit;
m_password=new QLineEdit;
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
m_connect=new QPushButton(tr("Connect"));
m_cancel=new QPushButton(tr("Cancel"));
QHBoxLayout *boutonLayout=new QHBoxLayout;
boutonLayout->addWidget(m_connect);
boutonLayout->addWidget(m_cancel);
//Creation d'un layout pour le logo
QLabel *logo=new QLabel(this);
logo->setPixmap(QPixmap("images/grandlogo.png"));
QHBoxLayout *logoLayout=new QHBoxLayout;
logoLayout->addWidget(logo);
//Creation d'un layout pour le message d'acceuil
m_label = new QLabel(tr("Welcome in callTracker"),this);
QHBoxLayout *labelLayout=new QHBoxLayout;
labelLayout->addWidget(m_label);
//Creation du layout principal de la fenetre
QVBoxLayout *layoutPrincipal=new QVBoxLayout;
//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(conn ectDB()));
connect(m_cancel,SIGNAL(clicked()),qApp, SLOT(quit()));
}
void FenetreAccueil::connectDB()
{
QString server;
server=m_addressBD->text();
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
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