当前位置:网站首页>[QT] test whether QT can connect to the database
[QT] test whether QT can connect to the database
2022-07-01 23:47:00 【Cappuccino-jay】
First step : Create a widget project
Then give the project file, that is .pro File to add QT += sql, As shown below :
The second step : Use 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

The third step : Connect to database
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QMYSQL");
// Set up ip、 port 、 name 、 password
db.setHostName("127.0.0.1");
db.setPort(3306);
db.setDatabaseName("mysql");
db.setUserName("root");
db.setPassword("root");
// Open database
bool res = db.open();
if(res){
qDebug()<< "connect success";
}
else{
qDebug()<< "connect error";
}
// Close the database
db.close();
}
Widget::~Widget()
{
delete ui;
}

Step four : Run the program

边栏推荐
- Concurrentskiplistmap -- principle of table skipping
- [must] bm41 output the right view of the binary tree [medium +]
- Algolia's search needs are almost closed
- Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
- MySQL: the difference between insert ignore, insert and replace
- - Oui. Env. Fichier XXX, avec constante, mais non spécifié
- BlocProvider为什么感觉和Provider很相似?
- 在长城证券上买基金安全吗?
- Use vb Net to convert PNG pictures into icon type icon files
- Material Design组件 - 使用BottomSheet展现扩展内容(一)
猜你喜欢

ARP报文头部格式和请求流程

【必会】BM41 输出二叉树的右视图【中等+】

BlocProvider为什么感觉和Provider很相似?

Deep learning | three concepts: epoch, batch, iteration

. env. XXX file, with constant, but undefined

Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c

E-commerce RPA robot helps brand e-commerce to achieve high traffic

Practical application and extension of plain framework

RPA教程01:EXCEL自动化从入门到实操

【QT】对于Qt MSVC 2017无法编译的问题解决
随机推荐
股票开户哪个证券公司最好,有安全保障吗
const // It is a const object... class nullptr_ t
How to solve the image pop-up problem when pycharm calls Matplotlib to draw
JPA handwritten SQL, received with user-defined entity classes
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
Regular expression collection
Notblank and notempty
第六章 数据流建模
How to realize parallel replication in MySQL replication
cookie、session、tooken
2021 robocom world robot developer competition - preliminary competition of undergraduate group
【QT】测试Qt是否能连接上数据库
2021 RoboCom 世界机器人开发者大赛-本科组初赛
字典、哈希表、数组的概念
Openwrt enable kV roaming
常见的积分商城游戏类型有哪些?
SecurityUtils.getSubject().getPrincipal()为null的问题怎么解决
边缘计算概述
Yunxin small class | common cognitive misunderstandings in IM and audio and video
2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板