当前位置:网站首页>[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
边栏推荐
- SQL optimization
- PyTorch学习记录
- sql 优化
- 【QT】测试Qt是否能连接上数据库
- The third part of the construction of the defense system of offensive and defensive exercises is the establishment of a practical security system
- vs2015 AdminDeployment.xml
- Key points and difficulties of the course "information content security" at Harbin Institute of Technology
- Stm32f030f4 drives tim1637 nixie tube chip
- De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
- PostgreSQL source code (57) why is the performance gap so large in hot update?
猜你喜欢
2021 RoboCom 世界机器人开发者大赛-本科组初赛
Current situation and future development trend of Internet of things
有没有一段代码,让你为人类的智慧所折服
门级建模—课后习题
Door level modeling - after class exercises
2021 robocom world robot developer competition - preliminary competition of higher vocational group
What category does the Internet of things application technology major belong to
Concurrentskiplistmap -- principle of table skipping
. env. XXX file, with constant, but undefined
Distance measurement - Hamming distance
随机推荐
openwrt 开启KV漫游
Create Ca and issue certificate through go language
PostgreSQL source code (57) why is the performance gap so large in hot update?
2021 robocom world robot developer competition - preliminary competition of higher vocational group
Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
Y53. Chapter III kubernetes from introduction to mastery -- ingress (26)
ADO. Net SqlDataAdapter object
Applet form verification encapsulation
JPA handwritten SQL, received with user-defined entity classes
[must] bm41 output the right view of the binary tree [medium +]
cookie、session、tooken
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
[LeetCode] 最后一个单词的长度【58】
What is the difference between memory leak and memory overflow?
[swoole Series 1] what will you learn in the world of swoole?
Various global files related to [.Net core] program
Key points and difficulties of the course "information content security" at Harbin Institute of Technology
【C#】依赖注入及Autofac
Linux foundation - centos7 offline installation of MySQL
Zero foundation tutorial of Internet of things development