#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDebug>
#include <QSqlError>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("IP");
db.setPort(3306);
db.setDatabaseName("表名");
db.setUserName("username");
db.setPassword("password");
bool ok = db.open();
if (ok)
{
// QMessageBox::information(this, "infor", "success");
qDebug()<<"success";
}
else
{
QMessageBox::information(this, "警告!", "DataBase Open Failed,Check whether there is a database or whether the parameters are correct!!!");
qDebug()<<"error open database because"<<db.lastError().text();
}
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::SelectAllPushTableData()
{
tableModel = new QSqlQueryModel;//定义一个数据库模型,指定父对象
QString strSelectData = "select * from qtdata";
tableModel->setQuery(strSelectData);
ui->tableView->setModel(tableModel);
}
void MainWindow::on_dis_clicked()
{
//查询所有并放入表中
SelectAllPushTableData();
//初始化表格 格式
ui->tableView->setColumnWidth(0,80);
ui->tableView->setColumnWidth(1,150);
ui->tableView->setColumnWidth(2,70);
ui->tableView->setColumnWidth(3,70);
ui->tableView->setColumnWidth(4,70);
ui->tableView->setColumnWidth(5,70);
ui->tableView->setColumnWidth(6,70);
ui->tableView->setColumnWidth(7,70);
setFixedSize(750,650);
setWindowTitle("——数据库管理平台——");
}
//清空表
bool MainWindow::clearDBTable()
{
QSqlQuery query;
QString strClearDB = "delete from qtdata";
query.prepare(strClearDB);
if(!query.exec())
{
qDebug()<<query.lastError();
}
return true;
}
void MainWindow::on_dle_clicked()
{
clearDBTable();
SelectAllPushTableData();
}
QT连接阿里云服务器的MySql数据库示例
需积分: 5 180 浏览量
2023-06-10
10:36:58
上传
评论 4
收藏 4KB 7Z 举报
LCH南安
- 粉丝: 101
- 资源: 10
最新资源
- tensorflow-gpu-2.7.4-cp37-cp37m-manylinux2010-x86-64.whl
- 多段线、 圆、弧转多段线(仅我可见)
- tensorflow-2.7.2-cp38-cp38-manylinux2010-x86-64.whl
- yeyue-p8Yi4-ve4a83792.apk
- tensorflow-gpu-2.7.3-cp38-cp38-manylinux2010-x86-64.whl
- 五相感应电机矢量控制模型MATLAB
- RGLED (1) (1).circ
- IMG_20240427_215747.jpg
- python下前端WEB学习笔记
- 田间种植行排号自动生成工具
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈