QT Creator有问题。我正在写一个程序,需要一个数据库。我选择了SQLITE,当我直接在MainWindow的构造函数中打开到数据库的连接时,一切正常。但是当我使用我的类Database或它的方法时,到数据库的连接不再工作,我得到错误
打开QSqlError时出错(“,“未加载驱动程序”,“未加载驱动程序”).
QtSql在示例、方法或指针方面是否有问题?
我将非常感谢提示,帮助或参考解决方案,因为我已经被困了大约12个小时了。
我的mainwindow.cpp时,它的工作。
#include "HeaderLibary.h"
#include "ui_mainwindow.h"
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setMaximumHeight(MAX_WINDOW_HEIGHT);
this->setMinimumHeight(MIN_WINDOW_HEIGHT);
this->setMaximumWidth(MAX_WINDOW_WIDTH);
this->setMinimumWidth(MIN_WINDOW_WIDTH);
QFont font;
font.setPixelSize(FONT_SIZE);
font.setBold(true);
font.setFamily(FONT_FAMILY);
QString buffer;
QGridLayout *windowLayout = new QGridLayout;
matrix *inputMatrix = new matrix("input",MAXLENGTH,false,QLINEEDIT_HEIGHT,QLINEEDIT_WIDTH,font,5,5,true,1);
inputMatrix->set_matrix();
this->inputWidgets = inputMatrix->getMatrixWidgets();
buffer = "Hello User, welcome to AES - Advanced Encryption Standard!";
QLabel *title = inputMatrix->setLabel(buffer,MAX_WINDOW_WIDTH,LABEL_HIGHT);
font.setPointSize(FONT_SIZE_TEXT);
buffer = "With AES you can encrypt messages of up to 16 characters or 16 Bytes."
"\n\nPlease enter your message in this matrix. One character per field in numbered order.";
QLabel *text = inputMatrix->setLabel(buffer,MAX_WINDOW_WIDTH,LABEL_HIGHT);
QPushButton *startBtn = inputMatrix->setButton("Start",BTN_WIDTH,BTN_HEIGHT);
connect(startBtn,SIGNAL(clicked()),this,SLOT(start_clicked()));
windowLayout->addWidget(title,0,0,1,2,Qt::AlignCenter);
windowLayout->addWidget(text,1,0,1,2,Qt::AlignCenter);
windowLayout->addLayout(inputMatrix->getLayout(),2,0,1,2,Qt::AlignCenter);
windowLayout->addWidget(startBtn,3,1,1,1,Qt::AlignRight);
QWidget *widget = new QWidget();
widget->setLayout(windowLayout);
setCentralWidget(widget);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("AES_DB.db");
if(!db.open())
qDebug()<< db.lastError();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::start_clicked()
{
QSqlDatabase db;
QSqlQuery qry;
QString tableName = "orginalMsgLetters";
QString buffer= QString("DROP TABLE %1;").arg(tableName);
if(!qry.exec(buffer))
qDebug()<< qry.lastError();
buffer = QString("CREATE TABLE IF NOT EXISTS %1 (id INTEGER NOT NULL PRIMARY KEY, itemValue VARCHAR(20));").arg(tableName);
if(!qry.exec(buffer))
qDebug()<< qry.lastError();
bool check = false;
int index = 0;
while(!check){
if(inputWidgets->at(index)->text() == ""){
QMessageBox::information(this,tr("Error"), tr("Please fill out each box to continue"));
return;
}
else if(inputWidgets->at(index)->text() == "ü" || inputWidgets->at(index)->text() == "ä" || inputWidgets->at(index)->text() == "ö" || inputWidgets->at(index)->text() == "ß"){
QMessageBox::information(this,tr("Error"), tr("Please don't use Ü/ü, Ä/ä, Ö/ö or ß "));
return;
}
else {
if(index+1 == MAXLENGTH)
check = true;
else
index++;
}
}
for (int i = 0; i < MAXLENGTH; ++i) {
buffer=QString("INSERT INTO %1 (id,itemValue)VALUES (%2,%3);").arg(tableName).arg(i).arg(this->inputWidgets->at(i)->text());
if(!qry.exec(buffer))
qDebug()<<qry.lastError();
buffer=QString("SELECT * FROM %1 WHERE id=%2").arg(tableName).arg(i);
if(!qry.exec(buffer))
qDebug()<<qry.lastError();
while(qry.next())
qDebug()<< qry.value(1).toString();
}
db.close();
}
我的主窗口. h
#pragma once
#include "HeaderLibary.h"
#include "Config_File.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
QList<QLineEdit*>* inputWidgets;
//Database *db = new Database();
QSqlDatabase *db = new QSqlDatabase;
QSqlQuery *qry = new QSqlQuery;
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void start_clicked();
private:
Ui::MainWindow *ui;
};
我的数据库. cpp
当我在我的mainwindow.cpp中使用这个的任何方法来初始化数据库时,它不会工作,我得到错误.“驱动程序未加载”
#include "database.h"
Database::Database(){
}
Database::~Database(){
}
void Database::deleteTable(QString tableName){
QSqlQuery qry;
QString buffer = QString("DROP TABLE %1;").arg(tableName);
qry.prepare(buffer);
qry.exec();
}
void Database::connect(){
QSqlDatabase db;
db.addDatabase("QSQLITE");
db.setDatabaseName("AES_DB.db");
if(!db.open())
qDebug()<< "Error open "<< db.lastError();
}
void Database::addValueToTable(QString tableName,int id, QString value){
QSqlQuery qry;
QString buffer = QString("INSERT INTO %1 (id,itemValue)VALUES (%2,%3);").arg(tableName).arg(id).arg(value);
qry.prepare(buffer);
if(!qry.exec()){
qDebug()<< qry.lastError();
qDebug()<< "\n insert Error \n";
}
}
void Database::createTable(QString tableName){
QSqlQuery qry;
QString buffer = QString("CREATE TABLE IF NOT EXISTS %1 (id INTEGER NOT NULL PRIMARY KEY, itemValue VARCHAR(20));").arg(tableName);
if(!qry.exec(buffer)){
qDebug()<< qry.lastError();
qDebug()<< "\n create Error \n";
}
}
void Database::close(){
QSqlDatabase db;
db.close();
}
QString Database::getValueFromTable(QString tableName,int id){
QSqlQuery qry;
QString string = QString("SELECT * FROM %1 WHERE id=%2").arg(tableName).arg(id);
qry.prepare(string);
if(!qry.exec(string)){
qDebug()<< qry.lastError();
qDebug()<< "\n get Error \n";
}
while(qry.next())
return qry.value(1).toString();
}
我的数据库. h
#pragma once
#include "Config_File.h"
#include "HeaderLibary.h"
class Database
{
private:
public:
Database();
~Database();
void connect();
void addValueToTable(QString tableName,int id, QString value);
void deleteTable (QString tableName);
void createTable(QString tableName);
QString getValueFromTable(QString tableName,int id);
void close();
};
'''
[1]: https://i.stack.imgur.com/48zP0.png
1条答案
按热度按时间2exbekwf1#
你为什么不用
...在您的Database::connect()方法中?