Qt+MySQL+MinGW32+更改MySQL数据库的存储位置+数据读取、显示、存储三线程代码
程序员文章站
2022-03-09 21:53:15
下载1.在这个安装包下载地址点击右侧【Looking for the latest GA version?】后,下载第二个,并安装。2.在这个下载地址,下载第二个配置下载后解压,将lib文件夹下的libmysql.dll和libmysql.lib复制到qt里......
下载
1.在这个安装包下载地址点击右侧【Looking for the latest GA version?】后,下载第二个,并安装。
2.在这个下载地址,下载32位的(两次下载的版本号最好对应)。
配置
下载后解压,将lib文件夹下的libmysql.dll和libmysql.lib复制到qt里
// qDebug() << loader.load();
//打印驱动名
//qDebug() << QSqlDatabase::drivers();
//添加一个数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
//设置数据库
db.setHostName("localhost");
db.setDatabaseName("mysql");//Mysql的名字
db.setUserName("root");//登录Mysql的用户名
db.setPassword("root");//登录Mysql的密码
//打开数据库
if(db.open() == false)
{
QMessageBox::warning(this, "warning", db.lastError().text());
}
else
{
qDebug() << true;
}
更改MySQL数据库的存储位置
- 将目录
datadir=C:/ProgramData/MySQL/MySQL Server 5.7/Data
下的文件复制到新的目录
datadir=D:\MySQL\data
- 将目录
C:\ProgramData\MySQL\MySQL Server 5.7
下的my.ini 文件的
datadir=C:/ProgramData/MySQL/MySQL Server 5.7/Data
更改为自己设定的路径:
datadir=D:\MySQL\data
这里使用了 \
- 重启电脑
出现错误::
LspTrace]ReadAccConfig returns false!
[LspTrace]FindMatchingProvider: inInfo is one of the LSPs entry.
[LspTrace]WSPStartup end: 0, protocol=17, [not] hook
[LspTrace]FindMatchingProvider: inInfo is one of the LSPs entry.
[LspTrace]WSPStartup end: 0, protocol=6, [not] hook
解决方法:
通过命令提示符修复LSP
1、打开电脑,进入命令提示符窗口,快捷键win+r.
2、在窗口中输入“cmd”进入命令符窗口。
3、在窗口中输入:输入netsh winsock reset,然后按下回车键。
4、然后稍等片刻,出现提示,重启电脑即可。
附上代码:
读取、显示、数据库保存 三线程
widgets.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlQuery>
//新建串口
void Widget::closeEvent(QCloseEvent* event)
{
//窗口关闭
if (threadDaq.isRunning())
{
threadDaq.terminate();//结束线程的run()函数执行
threadDaq.wait();//
}
if (threadShow.isRunning())
{
threadShow.terminate(); //因为threadB可能处于等待状态,所以用terminate强制结束
threadShow.wait();//
}
if (threadSave.isRunning())
{
threadSave.terminate(); //因为threadB可能处于等待状态,所以用terminate强制结束
threadSave.wait();//
}
event->accept();
}
Widget::Widget(QWidget* parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
// qDebug() << loader.load();
//打印驱动名
//qDebug() << QSqlDatabase::drivers();
//添加一个数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
//设置数据库
db.setHostName("localhost"); //主机IP
db.setDatabaseName("mydata");//Mysql的名字
db.setUserName("root");//登录Mysql的用户名
db.setPassword("root");//登录Mysql的密码
//打开数据库
if(db.open() == false)
{
QMessageBox::warning(this, "warning", db.lastError().text());
}
connect(&threadDaq, SIGNAL(started()), this, SLOT(onthreadDaq_started()));
connect(&threadDaq, SIGNAL(finished()), this, SLOT(onthreadDaq_finished()));
connect(&threadShow, SIGNAL(started()), this, SLOT(onthreadShow_started()));
connect(&threadShow, SIGNAL(finished()), this, SLOT(onthreadShow_finished()));
connect(&threadSave, SIGNAL(started()), this, SLOT(onthreadSave_started()));
connect(&threadSave, SIGNAL(finished()), this, SLOT(onthreadSave_finished()));
connect(&threadShow, SIGNAL(newValue(int, int )), this, SLOT(onthreadB_newValue(int, int )));
connect(&threadSave, SIGNAL(saveValue(int, int )), this, SLOT(onthreadB_saveValue(int, int )));
}
Widget::~Widget()
{
delete ui;
}
/*********************************槽函数*********************************/
void Widget::onthreadDaq_started()
{
ui->LabA->setText("ThreadDaq状态: started");
}
void Widget::onthreadDaq_finished()
{
ui->LabA->setText("ThreadDaq状态: finished");
}
void Widget::onthreadShow_started()
{
ui->LabB->setText("ThreadShow状态: started");
}
void Widget::onthreadShow_finished()
{
ui->LabB->setText("ThreadShow状态: finished");
}
void Widget::onthreadSave_started()
{
ui->LabC->setText("ThreadSave状态: started");
}
void Widget::onthreadSave_finished()
{
ui->LabC->setText("ThreadSave状态: finished");
}
void Widget::on_pushButton_clear_clicked()
{
ui->plainTextEdit->clear();
}
void Widget::onthreadB_newValue(int seq, int diceValue)
{
QString str = QString::asprintf("第 %d 次,数字为:%d", seq, diceValue);
ui->plainTextEdit->appendPlainText(str);
}
void Widget::onthreadB_saveValue(int seq, int diceValue)
{
//增删查改
//添加一条记录
QSqlQuery query;
QString s = QString::number(seq, 10);
QString val = QString::number(diceValue, 10);
QString sql = "insert into people (nums ,age) values('" + s + "'," + val + ")";
query .exec(sql);
}
/******************************控件按钮*********************************/
void Widget::on_pushButton_close_clicked()
{
//结束线程
// threadConsumer.stopThread();//结束线程的run()函数执行
threadDaq.terminate(); //因为threadB可能处于等待状态,所以用terminate强制结束
threadDaq.wait();//
threadSave.terminate();//结束线程的run()函数执行
threadSave.wait();//
threadShow.terminate();//结束线程的run()函数执行
threadShow.wait();//
ui->pushButton_start->setEnabled(true);
ui->pushButton_close->setEnabled(false);
}
void Widget::on_pushButton_start_clicked()
{
threadShow.start();
threadSave.start();
threadDaq.start();
ui->pushButton_start->setEnabled(false);
ui->pushButton_close->setEnabled(true);
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer>
#include "threads.h"
QT_BEGIN_NAMESPACE
namespace Ui
{
class Widget;
}
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
private:
threadDAQ threadDaq;
threadShow threadShow;
threadSave threadSave;
protected:
void closeEvent(QCloseEvent* event);
public:
Widget(QWidget* parent = nullptr);
~Widget();
private slots:
void onthreadDaq_started();
void onthreadDaq_finished();
void onthreadShow_started();
void onthreadShow_finished();
void onthreadSave_started();
void onthreadSave_finished();
void on_pushButton_clear_clicked();
void onthreadB_newValue(int seq, int val);
void onthreadB_saveValue(int seq, int val);
void on_pushButton_start_clicked();
void on_pushButton_close_clicked();
private:
Ui::Widget* ui;
};
#endif // WIDGET_H
threads.cpp
#include "threads.h"
#include <QWaitCondition>
#include <QTime>
#include <QMutex>
QMutex mutex;
QWaitCondition newdataAvailable;
int seq = 0; //序号
int diceValue;
/*********************************获取数据*******************************/
void threadDAQ::stopThread()
{
QMutexLocker locker(&mutex);
m_stop = true;
}
void threadDAQ::run()
{
m_stop = false; //启动线程时令m_stop=false
seq = 0;
qsrand(QTime::currentTime().msec());//随机数初始化,qsrand是线程安全的
while(!m_stop)//循环主体
{
mutex.lock();
diceValue = qrand(); //获取随机数
diceValue = (diceValue % 6) + 1;
seq++;
mutex.unlock();
newdataAvailable.wakeAll();//唤醒所有线程,有新数据了
msleep(500); //线程休眠
}
}
/*********************************显示数据*******************************/
threadShow::threadShow()
{
}
void threadShow::stopThread()
{
QMutexLocker locker(&mutex);
m_stop = true;
}
void threadShow::run()
{
m_stop = false; //启动线程时令m_stop=false
while(!m_stop)//循环主体
{
mutex.lock();
newdataAvailable.wait(&mutex);//会先解锁mutex,使其他线程可以使用mutex
emit newValue(seq, diceValue);
mutex.unlock();
// msleep(100); //线程休眠100ms
}
}
/*********************************保存数据*******************************/
threadSave::threadSave()
{
}
void threadSave::stopThread()
{
QMutexLocker locker(&mutex);
m_stop = true;
}
void threadSave::run()
{
m_stop = false; //启动线程时令m_stop=false
while(!m_stop)//循环主体
{
mutex.lock();
newdataAvailable.wait(&mutex);//会先解锁mutex,使其他线程可以使用mutex
emit saveValue(seq, diceValue);
mutex.unlock();
// msleep(100); //线程休眠100ms
}
}
threads.h
#ifndef THREADS_H
#define THREADS_H
//#include <QObject>
#include <QThread>
#include <QSerialPort>
#include <QTimer>
#include <QDebug>
#include <QSerialPortInfo>
/*********************************获取数据*******************************/
class threadDAQ : public QThread
{
Q_OBJECT
private:
bool m_stop = false; //停止线程
protected:
void run() Q_DECL_OVERRIDE;
public:
threadDAQ(QObject* parent = 0) : QThread(parent)
{
}
QSerialPort* m_serial;
void ThreadopenMycom();
void stopThread();
signals:
void ThreadMyCom(const QString&);
};
/*********************************显示数据*******************************/
class threadShow : public QThread
{
Q_OBJECT
private:
bool m_stop = false; //停止线程
protected:
void run() Q_DECL_OVERRIDE;
public:
threadShow();
void stopThread();
signals:
void newValue(int seq, int divceValue);
};
/*********************************存储数据*******************************/
class threadSave : public QThread
{
Q_OBJECT
private:
bool m_stop = false; //停止线程
protected:
void run() Q_DECL_OVERRIDE;
public:
threadSave();
void stopThread();
signals:
void saveValue(int seq, int diceValue);
};
#endif // THREADS_H
自己记录一下。。。。。。。。。。。。。
本文地址:https://blog.csdn.net/weixin_39308232/article/details/108563849