16,235
社区成员
发帖
与我相关
我的任务
分享
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#define FILE_NAME "ThreeKindoms.db"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
#if 1
//判断文件是否存在
QFileInfo fileInfo(QString(QCoreApplication::applicationDirPath() + "/" + FILE_NAME));
if (!fileInfo.exists()) {
//创建文件
this->createDBFile();
}
QString shuTable = "ShuKingdom";
this->createTable(shuTable);
#else
this->createDBFile();
#endif
}
Widget::~Widget()
{
delete ui;
}
void Widget::createDBFile()
{
m_database = QSqlDatabase::addDatabase("QSQLITE", "ThreeKindoms");
m_database.setDatabaseName(QString(QCoreApplication::applicationDirPath() + "/" + FILE_NAME));
if (!m_database.open())
qDebug() << "Open database file failed.";
else
qDebug() << "Open database file success.";
}
void Widget::createTable(QString &tableName)
{
if (!m_database.isOpen()) {
if (!m_database.open())
qDebug() << "ERROR:" << m_database.lastError();
}
……
}
Open database file success.
Create "ShuKingdom" success.
ERROR: QSqlError("", "Driver not loaded", "Driver not loaded")