时间:2022-06-23 11:40:08 | 栏目:C代码 | 点击:次
数据库同步的主要功能是将本地的数据库记录同步到远程的数据库,其中数据库类型不限,比如本地是sqlite数据库,远程可以是mysql数据库,本地是mysql数据库,远程也可以是postgresql数据库,只要数据库设置的时候设定好就行。这样做相当于就是通过程序的方式将数据库记录上传同步,还有一种机制是通过直接数据库上设置热备份啥的,个人还是习惯通过程序的方式,自主可控,比如可以自定义同步哪个表,哪部分数据需要同步。
有一种应用场景是现场很多客户端外接了各种传感器物联网设备(现在有个高大上的名字叫泛感知),客户端负责采集这些设备的数据,本地有记录,然后记录还可以同步到远程的数据库,需要提供一个app、网页、小程序啥的,去数据库中拿到对应的数据,如果没有这个上传到云端的过程,那就很难拿到这些数据,云端一般会有公网的IP地址,固定的,这样app也好小程序也好,直接发送请求回复对应的数据就行。除了能够拿到数据外,还可以发送对应的操作执行指令回控,比如单独一个表存储对应的指令,具体指令规则自定义,客户端在同步过程中还主动查询指令表是否有记录,有就取出来按照约定的规则执行,执行成功以后将对应记录删除。
数据库通信管理线程类特点:
体验地址:https://pan.baidu.com/s/15ZKAlptW-rDcNq8zlzdYLg 提取码:uyes 文件名:bin_dbtool.zip
国内站点:https://gitee.com/feiyangqingyun
国际站点:https://github.com/feiyangqingyun
#include "frmdbupload.h" #include "ui_frmdbupload.h" #include "quihelper.h" #include "dbconnthread.h" frmDbUpload::frmDbUpload(QWidget *parent) : QWidget(parent), ui(new Ui::frmDbUpload) { ui->setupUi(this); this->initForm(); this->initConfig(); } frmDbUpload::~frmDbUpload() { delete ui; } void frmDbUpload::initForm() { ui->frame->setFixedWidth(AppConfig::RightWidth); maxCount = 100; currentCount = 0; timer = new QTimer(this); timer->setInterval(1000); connect(timer, SIGNAL(timeout()), this, SLOT(on_btnDo_clicked())); //实例化数据库通信类 dbConn = new DbConnThread(this); dbConn->setDbFlag("云端"); connect(dbConn, SIGNAL(debug(QString)), this, SLOT(debug(QString))); connect(dbConn, SIGNAL(error(QString)), this, SLOT(error(QString))); } void frmDbUpload::initConfig() { ui->cboxDbType->addItems(DbHelper::getDbType()); ui->cboxDbType->setCurrentIndex(ui->cboxDbType->findText(AppConfig::DbType5)); connect(ui->cboxDbType, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); ui->txtDbName->setText(AppConfig::DbName5); connect(ui->txtDbName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); ui->txtHostName->setText(AppConfig::HostName5); connect(ui->txtHostName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); ui->txtHostPort->setText(QString::number(AppConfig::HostPort5)); connect(ui->txtHostPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); ui->txtUserName->setText(AppConfig::UserName5); connect(ui->txtUserName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); ui->txtUserPwd->setText(AppConfig::UserPwd5); connect(ui->txtUserPwd, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); } void frmDbUpload::saveConfig() { AppConfig::DbType5 = ui->cboxDbType->currentText(); AppConfig::DbName5 = ui->txtDbName->text(); AppConfig::HostName5 = ui->txtHostName->text(); AppConfig::HostPort5 = ui->txtHostPort->text().toInt(); AppConfig::UserName5 = ui->txtUserName->text(); AppConfig::UserPwd5 = ui->txtUserPwd->text(); AppConfig::writeConfig(); } void frmDbUpload::debug(const QString &msg) { DbHelper::appendMsg(ui->txtMain, maxCount, currentCount, 0, msg); } void frmDbUpload::error(const QString &msg) { DbHelper::appendMsg(ui->txtMain, maxCount, currentCount, 1, msg); } void frmDbUpload::on_btnOpen_clicked() { if (ui->btnOpen->text() == "打开数据库") { DbInfo dbInfo; dbInfo.connName = this->objectName(); dbInfo.dbName = AppConfig::DbName5; dbInfo.hostName = AppConfig::HostName5; dbInfo.hostPort = AppConfig::HostPort5; dbInfo.userName = AppConfig::UserName5; dbInfo.userPwd = AppConfig::UserPwd5; QString dbType = AppConfig::DbType5.toUpper(); if (dbType == "SQLITE") { dbInfo.dbName = DbHelper::getDbDefaultFile(); } dbConn->setConnInfo(DbHelper::getDbType(dbType), dbInfo); if (dbConn->openDb()) { dbConn->start(); ui->btnOpen->setText("关闭数据库"); } else { QString error = dbConn->getDatabase().lastError().text(); QUIHelper::showMessageBoxError("打开数据库失败!\n" + error, 3); } } else { dbConn->stop(); dbConn->closeDb(); ui->btnOpen->setText("打开数据库"); on_btnClear_clicked(); } QTimer::singleShot(100, this, SLOT(on_btnInit_clicked())); QTimer::singleShot(1000, this, SLOT(on_btnStart_clicked())); } void frmDbUpload::on_btnCopy_clicked() { //将数据库设置参数一键粘贴过来 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 frmDbUpload::on_btnInit_clicked() { if (!dbConn->getOk()) { return; } //填充默认设备的初始数据 QStringList sqls; //首先插入删除表语句 sqls << "delete from NodeData"; //再插入新建语句 for (int i = 1; i < 10; i++) { QString sql1 = QString("insert into NodeData(PositionID,NodeValue,NodeStatus,SaveTime)"); QString sql2 = QString("values('AT-1000%1','0',0,'%2')").arg(i).arg(DATETIME); sqls << (sql1 + " " + sql2); } dbConn->initDb(sqls); } void frmDbUpload::on_btnDo_clicked() { if (!dbConn->getOk()) { return; } //在软件中相应的更新语句(增加/删除/修改)插入到队列 //这里就举例更新值和时间 for (int i = 1; i < 10; i++) { int value1 = rand() % 100; int value2 = rand() % 5; QString sql = QString("update NodeData set NodeValue='%1',NodeStatus='%2',SaveTime='%3' where PositionID='AT-1000%4'") .arg(value1).arg(value2).arg(DATETIME).arg(i); dbConn->append(sql); } } void frmDbUpload::on_btnStart_clicked() { if (ui->btnStart->text() == "启动服务") { on_btnDo_clicked(); timer->start(); ui->btnStart->setText("停止服务"); } else { timer->stop(); ui->btnStart->setText("启动服务"); } } void frmDbUpload::on_btnClear_clicked() { DbHelper::appendMsg(ui->txtMain, maxCount, currentCount, 0, "", true); }