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

边栏推荐
- Concepts of dictionary, hash table and array
- Redis data types and application scenarios
- What are the common types of points mall games?
- 写给当前及未来博士研究生一些建议整理分享
- De PIP. Interne. CLI. Main Import main modulenotfounderror: No module named 'PIP'
- Applet form verification encapsulation
- Windows 7 install MySQL error: 1067
- SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
- Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
- ARP message header format and request flow
猜你喜欢

【QT】对于Qt MSVC 2017无法编译的问题解决

Is there a piece of code that makes you convinced by human wisdom

PostgreSQL source code (57) why is the performance gap so large in hot update?
![[es practice] safe operation mode on ES](/img/3f/fa28783770098ff10bffeccd64fe51.png)
[es practice] safe operation mode on ES

字典、哈希表、数组的概念

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

Using uni simple router, dynamically pass parameters typeerror: cannot convert undefined or null to object

Use pair to do unordered_ Key value of map

Yunxin small class | common cognitive misunderstandings in IM and audio and video

algolia 搜索需求,做的快自闭了...
随机推荐
Notes to problems - file /usr/share/mysql/charsets/readme from install of mysql-server-5.1.73-1 glibc23.x86_ 64 c
【必会】BM41 输出二叉树的右视图【中等+】
Notes on problems - /usr/bin/perl is needed by mysql-server-5.1.73-1 glibc23.x86_ sixty-four
How excel opens CSV files with more than one million lines
常见的积分商城游戏类型有哪些?
第六章 数据流建模
门级建模—课后习题
Matplotlib common charts
Use pair to do unordered_ Key value of map
Anomaly-Transformer (ICLR 2022 Spotlight)复现过程及问题
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
Zero foundation tutorial of Internet of things development
【ES实战】ES上的安全性运行方式
Which securities company is the best to open a stock account? Is there a security guarantee
Resumption of attack and defense drill
Why is PHP called hypertext preprocessor
Daily three questions 6.30 (2)
PostgreSQL notes (10) dynamically execute syntax parsing process
excel如何打开100万行以上的csv文件
Leetcode (34) -- find the first and last positions of elements in a sorted array