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

五、相關代碼
#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ù)據(jù)庫通信類
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 << "位號" << "當前值" << "狀態(tài)" << "時間";
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("用時( %1 秒) 標識( %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() == "打開數(shù)據(jù)庫") {
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("關閉數(shù)據(jù)庫");
} else {
QString error = dbConn->getDatabase().lastError().text();
QUIHelper::showMessageBoxError("打開數(shù)據(jù)庫失敗!\n" + error, 3);
}
} else {
dbConn->stop();
dbConn->closeDb();
ui->btnOpen->setText("打開數(shù)據(jù)庫");
on_btnClear_clicked();
}
QTimer::singleShot(1000, this, SLOT(on_btnStart_clicked()));
}
void frmDbDownload::on_btnCopy_clicked()
{
//將數(shù)據(jù)庫設置參數(shù)一鍵粘貼過來
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() == "啟動服務") {
on_btnDo_clicked();
timer->start();
ui->btnStart->setText("停止服務");
} else {
timer->stop();
ui->btnStart->setText("啟動服務");
}
}
void frmDbDownload::on_btnClear_clicked()
{
QUIHelper::appendMsg(ui->txtMain, 0, "", maxCount, currentCount);
}
到此這篇關于Qt數(shù)據(jù)庫應用之實現(xiàn)通用數(shù)據(jù)庫采集的文章就介紹到這了,更多相關Qt數(shù)據(jù)庫采集內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Windows下搭建FFmpeg開發(fā)調試環(huán)境的詳細步驟
這篇文章主要介紹了Windows下搭建FFmpeg開發(fā)調試環(huán)境,本文以VS2017為例一步步介紹怎么搭建一個可供單步調試的FFmpeg項目,需要的朋友可以參考下2022-07-07

