我遇到QSQLDATABASE问题。我正在使用Qt 4.8.5
我总是得到Driver not loaded错误。
我已经检查过QSQLITE驱动程序是否可用
到目前为止她是我的代码
article.h
#ifndef ARTICLE_H
#define ARTICLE_H
#include <QWidget>
#include <QtSql>
#include <QSqlDatabase>
namespace Ui {
class article;
}
class article : public QWidget
{
Q_OBJECT
public:
explicit article(QWidget *parent = 0);
~article();
void lastId(QString table);
bool changes();
void setChanges(bool change);
bool updates();
void setUpdates(bool update);
private slots:
void on_nouveaupushButton_clicked();
private:
Ui::article *ui;
bool m_detectChanges;
bool m_detectUpdates ;
};
#endif // ARTICLE_H
article.cpp
#include "article.h"
#include "ui_article.h"
#include <QMessageBox>
#include <databasemananger.h>
article::article(QWidget *parent) :
QWidget(parent),
ui(new Ui::article)
{
ui->setupUi(this);
m_detectChanges = false ;
m_detectUpdates = false;
// Setup
lastId("articles");
}
article::~article()
{
delete ui;
}
void article::lastId(QString table)
{
QSqlDatabase db = QSqlDatabase::database();
QSqlQuery query ;
QString queryString = "select seq from sqlite_sequence where name= ? ";
query.prepare(queryString);
query.addBindValue("articles");
if(!query.exec())
{
QMessageBox::critical(this,tr("Inventaire"),query.lastError().text());
return;
}
while(query.next())
{
ui->articleCodeLineEdit->setText("ART_" + QString::number(query.value(0).toInt() + 1));
return ;
}
if(ui->articleCodeLineEdit->text().isEmpty())
ui->articleCodeLineEdit->setText("ART_1");
}
bool article::changes()
{
return m_detectChanges ;
}
void article::setChanges(bool change)
{
m_detectChanges = change;
}
bool article::updates()
{
return m_detectUpdates;
}
void article::setUpdates(bool update)
{
m_detectUpdates = update ;
}
void article::on_nouveaupushButton_clicked()
{
// check changes
// get last id
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlDatabase>
#include <article.h>
#include <QtSql>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_actionAjouter_nouveau_article_triggered();
private:
Ui::MainWindow *ui;
QSqlDatabase *m_db ;
article *m_fenetreArticle;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QtGlobal>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_fenetreArticle = new article(this);
m_fenetreArticle->setWindowFlags(Qt::Window);
m_db = new QSqlDatabase;
// Base de données traitement
m_db->setHostName("localhost");
m_db->setDatabaseName("E:/apprendreQt/gestionstock6/database/gestionStock4.db");
m_db->setPassword("");
m_db->setUserName("");
if(!m_db->open())
QMessageBox::critical(this,"erreur connecting",m_db->lastError().text());
}
MainWindow::~MainWindow()
{
m_db->close();
QSqlDatabase::removeDatabase("gestionstock4.db");
delete ui;
}
void MainWindow::on_actionAjouter_nouveau_article_triggered()
{
m_fenetreArticle->show();
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
答案 0 :(得分:2)
请勿使用m_db = new QSqlDatabase;
。
请参阅documentation:
<强> QSqlDatabase :: QSqlDatabase()强>
创建一个空的无效QSqlDatabase对象。使用addDatabase(),removeDatabase()和database()来获取有效的QSqlDatabase对象。
指定调用addDatabase()
函数时要使用的驱动程序:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
答案 1 :(得分:0)
我发现问题是我的原因
m_fenetreArticle需要默认的数据库连接,我已经创建了 创建默认连接之前的m_fenetreArticle对象
mainwindow.cpp必须如此
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QtGlobal>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_db = new QSqlDatabase;
// Base de données traitement
m_db->setHostName("localhost");
m_db->setDatabaseName("E:/apprendreQt/gestionstock6/database/gestionStock4.db");
m_db->setPassword("");
m_db->setUserName("");
if(!m_db->open())
QMessageBox::critical(this,"erreur connecting",m_db->lastError().text());
m_fenetreArticle = new article(this);
m_fenetreArticle->setWindowFlags(Qt::Window);
}
MainWindow::~MainWindow()
{
m_db->close();
QSqlDatabase::removeDatabase("gestionstock4.db");
delete ui;
}
void MainWindow::on_actionAjouter_nouveau_article_triggered()
{
m_fenetreArticle->show();
}