当前位置:网站首页>[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
边栏推荐
- Practical application and extension of plain framework
- Write some suggestions to current and future doctoral students to sort out and share
- cookie、session、tooken
- ARP报文头部格式和请求流程
- 在长城证券上买基金安全吗?
- Stm32f030f4 drives tim1637 nixie tube chip
- Why does blocprovider feel similar to provider?
- URL introduction
- ConcurrentSkipListMap——跳表原理
- 攻防演练复盘
猜你喜欢
Redis AOF log
Pytorch learning record
Notblank and notempty
深度学习 | 三个概念:Epoch, Batch, Iteration
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
Practical application and extension of plain framework
Is there a piece of code that makes you convinced by human wisdom
The best smart home open source system in 2022: introduction to Alexa, home assistant and homekit ecosystem
Concurrentskiplistmap -- principle of table skipping
Zero foundation tutorial of Internet of things development
随机推荐
使用uni-simple-router,动态传参 TypeError: Cannot convert undefined or null to object
【C#】依赖注入及Autofac
2021 RoboCom 世界机器人开发者大赛-高职组复赛
Practical application and extension of plain framework
Iota in golang
ADO. Net SqlDataAdapter object
from pip._ internal. cli. main import main ModuleNotFoundError: No module named ‘pip‘
What professional classification does the application of Internet of things technology belong to
Reproduction process and problems of analog transformer (ICLR 2022 Spotlight)
【QT】对于Qt MSVC 2017无法编译的问题解决
Applet form verification encapsulation
How to solve the image pop-up problem when pycharm calls Matplotlib to draw
股票开户哪个证券公司最好,有安全保障吗
Postgresql源码(58)元组拼接heap_form_tuple剖析
Create Ca and issue certificate through go language
ADO.NET 之sqlConnection 对象使用摘要
[es practice] safe operation mode on ES
const // It is a const object...class nullptr_t
Write some suggestions to current and future doctoral students to sort out and share
PostgreSQL source code (58) tuple splicing heap_ form_ Tuple analysis