当前位置:网站首页>[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

边栏推荐
- Notblank and notempty
- ADO. Net SqlConnection object usage summary
- 2021 RoboCom 世界机器人开发者大赛-高职组复赛
- E-commerce RPA robot helps brand e-commerce to achieve high traffic
- Oracle中已定义者身份执行函数AUTHID DEFINER与Postgresql行为的异同
- 门级建模—课后习题
- Similarities and differences between the defined identity execution function authid determiner and PostgreSQL in Oracle
- Daily three questions 6.30
- 2022-07-01:某公司年会上,大家要玩一食发奖金游戏,一共有n个员工, 每个员工都有建设积分和捣乱积分, 他们需要排成一队,在队伍最前面的一定是老板
- 多表操作-一对一,一对多与多对多
猜你喜欢

ConcurrentSkipListMap——跳表原理

Deep learning | three concepts: epoch, batch, iteration

多表操作-一对一,一对多与多对多

2021 RoboCom 世界机器人开发者大赛-高职组复赛

Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four

- Oui. Env. Fichier XXX, avec constante, mais non spécifié

Zero foundation tutorial of Internet of things development

安全协议重点

Redis RDB快照

Why is PHP called hypertext preprocessor
随机推荐
图的遍历之深度优先搜索和广度优先搜索
What are the common types of points mall games?
2021 robocom world robot developer competition - semi finals of higher vocational group
Multi table operation - one to one, one to many and many to many
在代码中使用SqlCommand对象
边缘计算概述
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
Current situation and future development trend of Internet of things
SQL optimization
Daily three questions 6.30
【必会】BM41 输出二叉树的右视图【中等+】
Why is PHP called hypertext preprocessor
What professional classification does the application of Internet of things technology belong to
Windows 7 install MySQL error: 1067
有没有一段代码,让你为人类的智慧所折服
ADO. Net SqlDataAdapter object
RPA教程01:EXCEL自动化从入门到实操
Notblank and notempty
【QT】对于Qt MSVC 2017无法编译的问题解决
Door level modeling - after class exercises