当前位置:网站首页>【QT】测试Qt是否能连接上数据库
【QT】测试Qt是否能连接上数据库
2022-07-01 23:46:00 【Cappuccino-jay】
第一步:创建一个widget项目
然后给项目文件也就是.pro文件添加QT += sql,如下所示:
第二步:使用 QSqlDatabase
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QSqlDatabase>
#include <QDebug>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
QSqlDatabase db;
};
#endif // WIDGET_H

第三步:连接数据库
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QMYSQL");
//设置ip、端口、名字、密码
db.setHostName("127.0.0.1");
db.setPort(3306);
db.setDatabaseName("mysql");
db.setUserName("root");
db.setPassword("root");
//打开数据库
bool res = db.open();
if(res){
qDebug()<< "connect success";
}
else{
qDebug()<< "connect error";
}
//关闭数据库
db.close();
}
Widget::~Widget()
{
delete ui;
}

第四步:运行程序

边栏推荐
- sql 优化
- 问题随记 —— /usr/bin/perl is needed by MySQL-server-5.1.73-1.glibc23.x86_64
- .env.xxx 文件,加了常量,卻undefined
- 股票开户哪个证券公司最好,有安全保障吗
- Yunxin small class | common cognitive misunderstandings in IM and audio and video
- 第六章 数据流建模
- Overview of edge calculation
- Know --matplotlib
- 【无标题】
- Write some suggestions to current and future doctoral students to sort out and share
猜你喜欢
![[must] bm41 output the right view of the binary tree [medium +]](/img/a5/00b2f0df5ab448665a2b062d145e52.png)
[must] bm41 output the right view of the binary tree [medium +]

Use vb Net to convert PNG pictures into icon type icon files

2021 robocom world robot developer competition - preliminary competition of undergraduate group

Development trend and future direction of neural network Internet of things
![[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing](/img/08/9ecfd53a04e147022dde3449aec132.png)
[understanding of opportunity-35]: Guiguzi - flying clamp - the art of remote connection, remote control and remote testing

from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘

Redis AOF log

使用 pair 做 unordered_map 的键值

TS初次使用、ts类型

- Oui. Env. Fichier XXX, avec constante, mais non spécifié
随机推荐
sql 优化
PostgreSQL source code (57) why is the performance gap so large in hot update?
Practical application and extension of plain framework
BlocProvider为什么感觉和Provider很相似?
门级建模—课后习题
【ES实战】ES上的安全性运行方式
ARP message header format and request flow
Zero foundation tutorial of Internet of things development
Applet form verification encapsulation
[LeetCode] 最后一个单词的长度【58】
.env.xxx 文件,加了常量,却undefined
攻防演练复盘
深度学习 | 三个概念:Epoch, Batch, Iteration
ConcurrentSkipListMap——跳表原理
股票开户哪个证券公司最好,有安全保障吗
ADO. Net SqlDataAdapter object
Development trend and future direction of neural network Internet of things
ADO.NET之sqlCommand对象
13 MySQL-约束
MySQL Replication中并行复制怎么实现