In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to achieve general database synchronization in Qt". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve general database synchronization in Qt" can help you solve the problem.
I. Preface
The main function of database synchronization is to synchronize the local database records to the remote database, in which the database type is not limited, such as the local database is sqlite database, the remote database can be mysql database, the local database is mysql database, and the remote database is postgresql database, as long as the database is set up. This is equivalent to uploading database records for synchronization through a program, and another mechanism is to set up hot backups on the database directly. Individuals are still accustomed to self-control through the program, such as which table can be synchronized and which part of the data needs to be synchronized.
There is an application scenario in which many clients on the scene connect various sensors to Internet of things devices (now there is a high-end name called pan-sensing). The client is responsible for collecting data from these devices, which are recorded locally, and then the records can be synchronized to a remote database. You need to provide an app, web page, Mini Program, etc., go to the database to get the corresponding data, if there is no such process of uploading to the cloud. It is difficult to get these data. The cloud usually has a fixed IP address of the public network, so whether app or Mini Program, just send the request and reply to the corresponding data directly. In addition to getting the data, you can also send the corresponding operation execution instruction back control, for example, a single table stores the corresponding instruction, the specific instruction rules are customized, and the client actively queries whether there are records in the instruction table during synchronization. If so, take it out and execute it in accordance with the agreed rules. After successful execution, the corresponding records will be deleted.
Database communication management thread class features:
The database type can be set and multiple database types are supported.
Database types include but are not limited to odbc, sqlite, mysql, postgresql, sqlserver, oracle, people's Congress, etc.
Database connection information can be set, including host address, user information and so on.
With automatic reconnection mechanism, you can set whether to check the connection and the check interval.
Support a single queue of sql statements, which is generally used to query the returned data, one for each insert and one for execution.
Multiple sql statement queues are supported, which are generally used to submit data remotely, one at a time for execution.
Batch sql statement queues are supported, which are generally used to update data in batches. Multiple bars are inserted and executed at a time.
The maximum number of queues can be set to limit the collection of sql statements processed by the queue.
Print messages, error messages and query results are sent through the signal.
II. Functional features
At the same time, it supports a variety of databases such as odbc, sqlite, mysql, postgresql, sqlserver, oracle, people's Congress and so on.
A database class can not only manage local database communication, but also support remote database communication.
Database threads support the execution of various sql statements, including individual and batch statements.
All classes in the component print messages, error messages, and execution results are signaled.
Integrated database general page turning class (responsible for specific processing logic), with paging navigation controls (responsible for appearance), to form a super powerful page turning control.
Integrated database automatic cleaning class, set the maximum number of records to automatically clean up early data in the background.
Integrated custom delegate class, supporting check boxes, text boxes, drop-down boxes, date boxes, fine-tuning boxes, progress bars, and so on.
At the same time support Qt4-Qt6, test Qt4.6 to Qt6.3 any version, any system and compiler.
This component is trouble-free 360 days 7 times 24 hours running in at least tens of thousands of sites, commercial grade quality assurance.
Each class corresponds to a complete and detailed usage example, with detailed annotations, which is very suitable for reading and learning.
It can be run as a stand-alone program, such as automatically cleaning up early data and synchronizing data to the cloud.
All threading, no card interface, automatically reconnect the database.
General test case, sqlite database, database generator inserts 1000 records per second for about 0.003 seconds, while automatically cleans up data classes and deletes 1000 records per second for about 0.13s. Different threads do not interfere with each other.
Third, experience address
Experience address: https://pan.baidu.com/s/15ZKAlptW-rDcNq8zlzdYLg extraction code: uyes file name: bin_dbtool.zip
Domestic site: https://gitee.com/feiyangqingyun
International site: https://github.com/feiyangqingyun
4. Effect picture
Related code # 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 (); / / instantiated database communication class dbConn = new DbConnThread (this); dbConn- > setDbFlag ("cloud"); 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 (); saveConfig > ui- > ui- (ui-) 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 () = = "Open database") {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 ("shut down database") } else {QString error = dbConn- > getDatabase (). LastError (). Text (); QUIHelper::showMessageBoxError ("failed to open database!\ n" + error, 3);}} else {dbConn- > stop (); dbConn- > closeDb (); ui- > btnOpen- > setText ("Open database"); on_btnClear_clicked () } QTimer::singleShot (100,100, this, SLOT (on_btnInit_clicked ()); QTimer::singleShot (1000, this, SLOT (on_btnStart_clicked ();} void frmDbUpload::on_btnCopy_clicked () {/ / paste database setting parameters to 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;} / / populate the initial data of the default device QStringList sqls; / / first insert the delete table statement sqls getOk ()) {return } / / insert the corresponding update statement (add / delete / modify) into the queue / / here is an example to update the value and time for (int I = 1; I) in the software
< 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 () = = "start service") {on_btnDo_clicked (); timer- > start (); ui- > btnStart- > setText ("stop service");} else {timer- > stop (); ui- > btnStart- > setText ("start service") }} void frmDbUpload::on_btnClear_clicked () {DbHelper::appendMsg (ui- > txtMain, maxCount, currentCount, 0, ", true);} this is the end of the introduction on" how to achieve universal database synchronization in Qt ". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.