Qt數(shù)據(jù)庫(kù)應(yīng)用之實(shí)現(xiàn)通用數(shù)據(jù)庫(kù)采集
一、前言
數(shù)據(jù)庫(kù)采集對(duì)應(yīng)的就是上一篇文章的數(shù)據(jù)庫(kù)同步,數(shù)據(jù)庫(kù)同步到云端數(shù)據(jù)庫(kù)以后,app、網(wǎng)頁(yè)、小程序啥的要數(shù)據(jù)的話,可以通過(guò)執(zhí)行http請(qǐng)求拿到數(shù)據(jù),http接收應(yīng)答這邊程序一般最簡(jiǎn)單可以用php寫,幾行代碼即可,比如根據(jù)請(qǐng)求的參數(shù)構(gòu)建需要執(zhí)行的sql語(yǔ)句,執(zhí)行以后將結(jié)果json形式返回即可。
通過(guò)Qt程序去做數(shù)據(jù)庫(kù)采集有多種方式,即可直接連接數(shù)據(jù)庫(kù)然后綁定數(shù)據(jù)庫(kù)表到QSqlTableModel,然后不斷調(diào)用select方法查詢刷新界面即可,也可通過(guò)執(zhí)行sql語(yǔ)句的形式,通過(guò)sql語(yǔ)句的形式更靈活,可以先拿到數(shù)據(jù)結(jié)果集合,然后過(guò)濾,比如判斷是否報(bào)警了,是否需要執(zhí)行其他指令動(dòng)作,然后再將需要的數(shù)據(jù)信號(hào)的形式發(fā)出去,需要的地方關(guān)聯(lián)信號(hào)拿到數(shù)據(jù)展示即可,相當(dāng)于做了一層篩選。
數(shù)據(jù)庫(kù)通信管理線程類特點(diǎn):
- 可設(shè)置數(shù)據(jù)庫(kù)類型,支持多種數(shù)據(jù)庫(kù)類型。
- 數(shù)據(jù)庫(kù)類型包括但不限于odbc、sqlite、mysql、postgresql、sqlserver、oracle、人大金倉(cāng)等。
- 可設(shè)置數(shù)據(jù)庫(kù)連接信息包括主機(jī)地址、用戶信息等。
- 具有自動(dòng)重連機(jī)制,可設(shè)置是否檢查連接以及檢查間隔。
- 支持單條sql語(yǔ)句隊(duì)列,一般用于查詢返回?cái)?shù)據(jù),每次插入一條執(zhí)行一條。
- 支持多條sql語(yǔ)句隊(duì)列,一般用于遠(yuǎn)程提交數(shù)據(jù),每次插入一條執(zhí)行多條。
- 支持批量sql語(yǔ)句隊(duì)列,一般用于批量更新數(shù)據(jù),每次插入多條執(zhí)行多條。
- 可設(shè)置隊(duì)列最大數(shù)量,限定排隊(duì)處理的sql語(yǔ)句集合。
- 通過(guò)信號(hào)發(fā)出 打印信息、錯(cuò)誤信息、查詢結(jié)果。
關(guān)于Qt數(shù)據(jù)庫(kù)相關(guān)開(kāi)發(fā)的一些經(jīng)驗(yàn)總結(jié)
二、功能特點(diǎn)
同時(shí)支持多種數(shù)據(jù)庫(kù)比如odbc、sqlite、mysql、postgresql、sqlserver、oracle、人大金倉(cāng)等。
一個(gè)數(shù)據(jù)庫(kù)類即可管理本地?cái)?shù)據(jù)庫(kù)通信,也支持遠(yuǎn)程數(shù)據(jù)庫(kù)通信等。
數(shù)據(jù)庫(kù)線程支持執(zhí)行各種sql語(yǔ)句,包括單條和批量。
組件中的所有類打印信息、錯(cuò)誤信息、執(zhí)行結(jié)果都信號(hào)發(fā)出去。
集成數(shù)據(jù)庫(kù)通用翻頁(yè)類(負(fù)責(zé)具體處理邏輯),搭配分頁(yè)導(dǎo)航控件(負(fù)責(zé)外觀),形成超級(jí)牛逼的翻頁(yè)控件。
集成數(shù)據(jù)庫(kù)自動(dòng)清理類,設(shè)定最大記錄數(shù)后臺(tái)自動(dòng)清理早期數(shù)據(jù)。
集成自定義委托類,支持復(fù)選框、文本框、下拉框、日期框、微調(diào)框、進(jìn)度條等。
同時(shí)支持Qt4-Qt6,親測(cè)Qt4.6到Qt6.3任意版本,任意系統(tǒng)和編譯器。
本組件無(wú)故障 360天7乘24小時(shí) 運(yùn)行在至少上萬(wàn)個(gè)現(xiàn)場(chǎng),商業(yè)級(jí)別品質(zhì)保證。
每個(gè)類都對(duì)應(yīng)完整詳細(xì)的使用示例,注釋詳細(xì),非常適合閱讀學(xué)習(xí)。
可以作為獨(dú)立的程序運(yùn)行,比如自動(dòng)清理早期數(shù)據(jù),同步數(shù)據(jù)到云端。
全部線程處理,不卡界面,自動(dòng)重連數(shù)據(jù)庫(kù)。
普通測(cè)試情況,sqlite數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)發(fā)生器每秒鐘插入1000條記錄約0.003秒鐘,同時(shí)自動(dòng)清理數(shù)據(jù)類每秒鐘刪除1000條記錄約0.13秒,不同線程互不干擾。
三、體驗(yàn)地址
體驗(yàn)地址:https://pan.baidu.com/s/15ZKAlptW-rDcNq8zlzdYLg 提取碼:uyes 文件名:bin_dbtool.zip
國(guó)內(nèi)站點(diǎn):https://gitee.com/feiyangqingyun
國(guó)際站點(diǎn):https://github.com/feiyangqingyun
四、效果圖

五、相關(guān)代碼
#include "frmdbdownload.h"
#include "ui_frmdbdownload.h"
#include "quihelper.h"
#include "dbconnthread.h"
frmDbDownload::frmDbDownload(QWidget *parent) : QWidget(parent), ui(new Ui::frmDbDownload)
{
ui->setupUi(this);
this->initForm();
this->initConfig();
this->initTable();
}
frmDbDownload::~frmDbDownload()
{
delete ui;
}
void frmDbDownload::initForm()
{
ui->frame->setFixedWidth(AppConfig::RightWidth);
QUIHelper::initTableView(ui->tableWidget);
maxCount = 100;
currentCount = 0;
columnCount = 0;
timer = new QTimer(this);
timer->setInterval(1000);
connect(timer, SIGNAL(timeout()), this, SLOT(on_btnDo_clicked()));
//實(shí)例化數(shù)據(jù)庫(kù)通信類
dbConn = new DbConnThread(this);
dbConn->setDbFlag("云端");
connect(dbConn, SIGNAL(debug(QString)), this, SLOT(debug(QString)));
connect(dbConn, SIGNAL(error(QString)), this, SLOT(error(QString)));
connect(dbConn, SIGNAL(receiveData(QString, QStringList, int)), this, SLOT(receiveData(QString, QStringList, int)));
}
void frmDbDownload::initConfig()
{
ui->cboxDbType->addItems(DbHelper::getDbType());
ui->cboxDbType->setCurrentIndex(ui->cboxDbType->findText(AppConfig::DbType6));
connect(ui->cboxDbType, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig()));
ui->txtDbName->setText(AppConfig::DbName6);
connect(ui->txtDbName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtHostName->setText(AppConfig::HostName6);
connect(ui->txtHostName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtHostPort->setText(QString::number(AppConfig::HostPort6));
connect(ui->txtHostPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtUserName->setText(AppConfig::UserName6);
connect(ui->txtUserName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
ui->txtUserPwd->setText(AppConfig::UserPwd6);
connect(ui->txtUserPwd, SIGNAL(textChanged(QString)), this, SLOT(saveConfig()));
}
void frmDbDownload::saveConfig()
{
AppConfig::DbType6 = ui->cboxDbType->currentText();
AppConfig::DbName6 = ui->txtDbName->text();
AppConfig::HostName6 = ui->txtHostName->text();
AppConfig::HostPort6 = ui->txtHostPort->text().toInt();
AppConfig::UserName6 = ui->txtUserName->text();
AppConfig::UserPwd6 = ui->txtUserPwd->text();
AppConfig::writeConfig();
}
void frmDbDownload::initTable()
{
QStringList columnNames;
columnNames << "位號(hào)" << "當(dāng)前值" << "狀態(tài)" << "時(shí)間";
QList<int> columnWidths;
columnWidths << 160 << 100 << 100 << 220;
columnCount = columnNames.count();
ui->tableWidget->setColumnCount(columnCount);
ui->tableWidget->setHorizontalHeaderLabels(columnNames);
for (int i = 0; i < columnCount; i++) {
ui->tableWidget->setColumnWidth(i, columnWidths.at(i));
}
}
void frmDbDownload::debug(const QString &msg)
{
QUIHelper::appendMsg(ui->txtMain, 0, msg, maxCount, currentCount);
}
void frmDbDownload::error(const QString &msg)
{
QUIHelper::appendMsg(ui->txtMain, 1, msg, maxCount, currentCount);
}
void frmDbDownload::receiveData(const QString &tag, const QStringList &data, int msec)
{
QString msg = QString("用時(shí)( %1 秒) 標(biāo)識(shí)( %2 ) 數(shù)據(jù)( %3 )").arg(QString::number((double)msec / 1000, 'f', 3)).arg(tag).arg(data.join("|"));
QUIHelper::appendMsg(ui->txtMain, 3, msg, maxCount, currentCount);
//顯示到表格中
int count = data.count();
ui->tableWidget->setRowCount(count / columnCount);
int row = -1;
for (int i = 0; i < count; i = i + columnCount) {
row++;
for (int j = 0; j < columnCount; ++j) {
ui->tableWidget->setItem(row, j, new QTableWidgetItem(data.at(i + j)));
}
}
}
void frmDbDownload::on_btnOpen_clicked()
{
if (ui->btnOpen->text() == "打開(kāi)數(shù)據(jù)庫(kù)") {
DbInfo dbInfo;
dbInfo.connName = this->objectName();
dbInfo.dbName = AppConfig::DbName6;
dbInfo.hostName = AppConfig::HostName6;
dbInfo.hostPort = AppConfig::HostPort6;
dbInfo.userName = AppConfig::UserName6;
dbInfo.userPwd = AppConfig::UserPwd6;
QString dbType = AppConfig::DbType6.toUpper();
if (dbType == "SQLITE") {
dbInfo.dbName = DbHelper::getDbDefaultFile();
}
dbConn->setConnInfo(DbHelper::getDbType(dbType), dbInfo);
if (dbConn->openDb()) {
dbConn->start();
ui->btnOpen->setText("關(guān)閉數(shù)據(jù)庫(kù)");
} else {
QString error = dbConn->getDatabase().lastError().text();
QUIHelper::showMessageBoxError("打開(kāi)數(shù)據(jù)庫(kù)失敗!\n" + error, 3);
}
} else {
dbConn->stop();
dbConn->closeDb();
ui->btnOpen->setText("打開(kāi)數(shù)據(jù)庫(kù)");
on_btnClear_clicked();
}
QTimer::singleShot(1000, this, SLOT(on_btnStart_clicked()));
}
void frmDbDownload::on_btnCopy_clicked()
{
//將數(shù)據(jù)庫(kù)設(shè)置參數(shù)一鍵粘貼過(guò)來(lái)
ui->cboxDbType->setCurrentIndex(ui->cboxDbType->findText(AppConfig::LocalDbType));
ui->txtDbName->setText(AppConfig::LocalDbName);
ui->txtHostName->setText(AppConfig::LocalHostName);
ui->txtHostPort->setText(QString::number(AppConfig::LocalHostPort));
ui->txtUserName->setText(AppConfig::LocalUserName);
ui->txtUserPwd->setText(AppConfig::LocalUserPwd);
}
void frmDbDownload::on_btnDo_clicked()
{
if (!dbConn->getOk()) {
return;
}
dbConn->select("NodeData", "PositionID,NodeValue,NodeStatus,SaveTime");
}
void frmDbDownload::on_btnStart_clicked()
{
if (ui->btnStart->text() == "啟動(dòng)服務(wù)") {
on_btnDo_clicked();
timer->start();
ui->btnStart->setText("停止服務(wù)");
} else {
timer->stop();
ui->btnStart->setText("啟動(dòng)服務(wù)");
}
}
void frmDbDownload::on_btnClear_clicked()
{
QUIHelper::appendMsg(ui->txtMain, 0, "", maxCount, currentCount);
}
到此這篇關(guān)于Qt數(shù)據(jù)庫(kù)應(yīng)用之實(shí)現(xiàn)通用數(shù)據(jù)庫(kù)采集的文章就介紹到這了,更多相關(guān)Qt數(shù)據(jù)庫(kù)采集內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Qt數(shù)據(jù)庫(kù)應(yīng)用之通用數(shù)據(jù)庫(kù)同步
- Qt數(shù)據(jù)庫(kù)應(yīng)用之實(shí)現(xiàn)通用數(shù)據(jù)庫(kù)分頁(yè)
- Qt數(shù)據(jù)庫(kù)應(yīng)用之實(shí)現(xiàn)通用數(shù)據(jù)庫(kù)清理
- Qt數(shù)據(jù)庫(kù)相關(guān)應(yīng)用開(kāi)發(fā)總結(jié)
- Qt數(shù)據(jù)庫(kù)應(yīng)用之實(shí)現(xiàn)通用數(shù)據(jù)生成器
- Qt數(shù)據(jù)庫(kù)應(yīng)用之實(shí)現(xiàn)數(shù)據(jù)圖文混排
- Qt數(shù)據(jù)庫(kù)應(yīng)用之實(shí)現(xiàn)數(shù)據(jù)打印到紙張
相關(guān)文章
C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)鏈表逆序并輸出
這篇文章主要介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)鏈表逆序并輸出的相關(guān)資料,需要的朋友可以參考下2017-04-04
C/C++中CJSON的使用(創(chuàng)建與解析JSON數(shù)據(jù))
cJSON是一個(gè)超輕巧的JSON解析器,本文主要介紹了C/C++中CJSON的使用(創(chuàng)建與解析JSON數(shù)據(jù)),具有一定的參考價(jià)值,感興趣的可以了解一下2021-09-09
基于linux下C開(kāi)發(fā)中的幾點(diǎn)技術(shù)經(jīng)驗(yàn)總結(jié)
本篇文章是對(duì)linux下C開(kāi)發(fā)中的幾點(diǎn)技術(shù)經(jīng)驗(yàn)總結(jié)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語(yǔ)言?詳解如何刪除有序數(shù)組中的重復(fù)項(xiàng)
數(shù)組不擅長(zhǎng)插入(添加)和刪除元素。數(shù)組的優(yōu)點(diǎn)在于它是連續(xù)的,所以查找數(shù)據(jù)速度很快。但這也是它的一個(gè)缺點(diǎn)。正因?yàn)樗沁B續(xù)的,所以當(dāng)插入一個(gè)元素時(shí),插入點(diǎn)后所有的元素全部都要向后移;而刪除一個(gè)元素時(shí),刪除點(diǎn)后所有的元素全部都要向前移2022-03-03

