#include <QMutex>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QtSql/QSqlIndex>
#include <QtSql/QSqlRecord>
#include <QTextStream>
#include <QStringList>
#include <QVariant>
#include <windows.h>
#include <QFile>
#include <QFileDialog>
#pragma execution_character_set("utf-8")
struct TEST_DATA
{
int ID;
int site;
int PartID;
double A;
double B;
double C;
double D;
double E;
};
class MyDataBase
{
public:
MyDataBase();
~MyDataBase();
private:
MyDataBase(const MyDataBase &);
MyDataBase operator =(const MyDataBase &);
private:
static MyDataBase *m_pInstance;
public:
static MyDataBase * GetInstance();
bool ConnectAccessDB(const QString &strDBName) const;
public:
QSqlDatabase GetDatabase() const
{
return QSqlDatabase::database("MyAccessDB");
}
QSqlQuery GetSqlQuery() const
{
static QSqlQuery query(m_pInstance->GetDatabase());
return query;
}
bool IsValid() const
{
return this->GetDatabase().isValid();
}
bool IsConnected() const
{
return this->GetDatabase().isOpen();
}
};
#include "ReadDataBase.h"
#include <QDebug>
MyDataBase * MyDataBase::m_pInstance(NULL);
MyDataBase::MyDataBase()
{
}
MyDataBase::~MyDataBase()
{
this->GetDatabase().close();
}
MyDataBase MyDataBase::operator=(const MyDataBase &)
{
return MyDataBase();
}
MyDataBase * MyDataBase::GetInstance()
{
if (NULL == m_pInstance)
{
static QMutex mutex;
mutex.lock();
if (NULL == m_pInstance)
{
static MyDataBase myDB;
m_pInstance = &myDB;
}
mutex.unlock();
}
return m_pInstance;
}
bool MyDataBase::ConnectAccessDB(const QString &strDBName) const
{
qDebug() << "支持的数据库驱动: " << QSqlDatabase::drivers();
QSqlDatabase db;
if (QSqlDatabase::contains("MyAccessDB"))
{
db = QSqlDatabase::database("MyAccessDB");
}
else
{
db = QSqlDatabase::addDatabase("QODBC", "MyAccessDB");
}
const QString strName(QString("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%1").arg(strDBName));
db.setDatabaseName(strName);
if (!db.isValid())
{
return false;
}
if (db.isOpen())
{
return true;
}
if (db.open())
{
qDebug() << "打开数据库成功!";
return true;
}
else
{
qDebug() << db.lastError().text();
return false;
}
}
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_MainWindow.h"
#include"ReadDataBase.h"
#include<QList>
#include<QFile>
#include<QTextStream>
#include<QDir>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
QList<TEST_DATA> m_lstTestData;
public slots:
void OnGetAccessData();
private:
MyDataBase *p_ParamWrite;
private:
Ui::MainWindowClass ui;
};
#include "MainWindow.h"
#include<QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
connect(ui.pushButton_getdara, SIGNAL(clicked()), this, SLOT(OnGetAccessData()));
p_ParamWrite = MyDataBase::GetInstance();
QString filePath = "E:/数据库/testData.mdb";
bool ret = p_ParamWrite->ConnectAccessDB(filePath);
if (ret == false)
{
qDebug() << "打开数据库失败!";
}
else
{
qDebug() << "打开数据库成功!";
}
}
MainWindow::~MainWindow()
{}
void MainWindow::OnGetAccessData()
{
QFile file(qApp->applicationDirPath() + QDir::separator() + "data.txt");
bool isOK = file.open(QIODevice::WriteOnly);
if (!isOK)
{
return;
}
QTextStream OutFile(&file);
QSqlDatabase db = p_ParamWrite->GetDatabase();
if (!db.isOpen())
{
qDebug() << "获取数据库失败!";
return;
}
QStringList tables;
QString tabName, sqlString;
tables = db.tables(QSql::Tables);
qDebug() << "tables:" << tables;
sqlString = QStringLiteral("select * from DutTable");
QSqlQuery q(sqlString, db);
QSqlRecord rec = q.record();
int fieldCount = rec.count();
qDebug() << "表列数: " << fieldCount;
QString fieldName;
for (int j = 0; j < fieldCount; j++)
{
fieldName = rec.fieldName(j);
qDebug() << j << fieldName;
}
m_lstTestData.clear();
auto setdecimal = [](double val)->QString
{
return QString::number(val, 'f', 3);
};
while (q.next())
{
TEST_DATA data;
data.ID = q.value(0).toInt();
data.site = q.value(1).toInt();
data.PartID = q.value(2).toInt();
data.A = q.value(3).toDouble();
data.B = q.value(4).toDouble();
data.C = q.value(5).toDouble();
data.D = q.value(6).toDouble();
data.E = q.value(7).toDouble();
m_lstTestData.append(data);
OutFile << data.ID << "," << data.site << "," << data.PartID << ","
<< setdecimal(data.A)<< "," << setdecimal(data.B) << "," << setdecimal(data.C)<< "," << setdecimal( data.D) << "," << setdecimal( data.E )<< endl;
}
file.close();
}