Qt监控Access数据库
配置文件setup.ini内容
[General] DBFilePath=C:/Users/WangGang/Desktop/Database1.accdb DBUserName= DBPassword= GameIp=192.168.1.152 GamePort=3000 4DSystemIp=192.168.1.152 4DSystemPort=21392 UdpDataToGame=StartGame UdpDataTo4DSystem=AS%NPlayDirect%1%
工程文件中QT+=sql
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QUdpSocket> #include <QtSql> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void connectDB(); void sendUdpData(); private slots: void on_pushButton_clicked(); void queryDB(); void readUdpData(); private: Ui::MainWindow *ui; QTimer* queryTimer;//监测数据库定时器 QUdpSocket* udpSocket; QSqlDatabase db; QString DBFilePath;//数据库文件路径 QString DBUserName;//数据库用户名 QString DBPassword;//数据库密码 QString GameIp;//游戏主机IP quint16 GamePort;//游戏UDP绑定的端口 QString FourDSystemIp;//4D系统IP quint16 FourDSystemPort;//4D系统UDP绑定的端口 QString UdpDataToGame;//向游戏发送的UDP数据 QString UdpDataTo4DSystem;//向4D系统发送的UDP数据 }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QString appDirPath = QCoreApplication::applicationDirPath(); QSettings* configIniRead = new QSettings(appDirPath+"/setup.ini", QSettings::IniFormat); configIniRead->setIniCodec(QTextCodec::codecForName("GB2312")); if( configIniRead->allKeys().size() > 0 ) { DBFilePath = configIniRead->value("DBFilePath").toString(); DBUserName = configIniRead->value("DBUserName").toString(); DBPassword = configIniRead->value("DBPassword").toString(); GameIp = configIniRead->value("GameIp").toString(); GamePort = configIniRead->value("GamePort").toUInt(); qDebug()<<GameIp<<GamePort; FourDSystemIp = configIniRead->value("4DSystemIp").toString(); FourDSystemPort = configIniRead->value("4DSystemPort").toUInt(); UdpDataToGame = configIniRead->value("UdpDataToGame").toString(); UdpDataTo4DSystem = configIniRead->value("UdpDataTo4DSystem").toString(); qDebug()<<"DBFilePath:"<<DBFilePath; qDebug()<<"DBUserName:"<<DBUserName; } else { qDebug() << "have no setup.ini , use default setting ." ; } connectDB(); udpSocket = new QUdpSocket(this); connect( udpSocket, SIGNAL(readyRead()), SLOT(readUdpData()) ); queryTimer = new QTimer(this); //connect( queryTimer, SIGNAL(timeout()), SLOT(queryDB()) ); queryTimer->start(); } MainWindow::~MainWindow() { delete ui; db.close(); } //创建数据库连接 void MainWindow::connectDB() { QString sDbNm = DBFilePath;//数据库文件 db = QSqlDatabase::addDatabase("QODBC");//设置数据库驱动 QString dsn = QString("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; FIL={MS Access};DBQ=%1;").arg(sDbNm);//连接字符串 db.setDatabaseName(dsn);//设置连接字符串 db.setUserName(DBUserName);//设置登陆数据库的用户名 db.setPassword(DBPassword);//设置密码 bool ok = db.open(); if (!ok) { QMessageBox messageBox; messageBox.setText("Database error"); messageBox.exec(); db.close(); } } //查询数据库数据 void MainWindow::queryDB() { QSqlQuery query; query.prepare("select * from t1"); query.exec(); while (query.next()) { qDebug()<<query.value(1).toString(); //udpSocket->writeDatagram(data, sender, senderPort); } query.clear(); } void MainWindow::sendUdpData() { udpSocket->writeDatagram(UdpDataToGame.toStdString().data(), QHostAddress(GameIp), GamePort); //qDebug()<<GameIp<<GamePort; udpSocket->writeDatagram(UdpDataTo4DSystem.toStdString().data(), QHostAddress(FourDSystemIp), FourDSystemPort); } void MainWindow::readUdpData() { QByteArray data; QHostAddress sender; quint16 senderPort = 0; while ( udpSocket->hasPendingDatagrams() ) { data.resize( udpSocket->pendingDatagramSize() ); udpSocket->readDatagram( data.data(), data.size(), &sender, &senderPort ); // DECLARE_DATA_READER(data, command); // switch ( command ) // { // case COMMAND_START_APP: onCmdStartApp(sender, senderPort, dataStream);break; // case COMMAND_STOP_APP: onCmdStopApp(sender, senderPort, dataStream);break; // case COMMAND_START_GAME: onCmdStartGame(sender, senderPort, dataStream);break; // case COMMAND_PULSE: onCmdPulse(sender, senderPort, dataStream);break; // case COMMAND_SHUTDOWN: onCmdShutdown(sender, senderPort, dataStream);break; // default: break; // } } } void MainWindow::on_pushButton_clicked() { queryDB(); sendUdpData(); }