我对 QSQLDATABASE 有疑问。我正在使用 Qt 4.8.5
我总是得到驱动程序未加载错误。
我检查了 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
文章.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
}
主窗口.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
主窗口.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();
}
主文件
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}