当前位置: 首页 > news >正文

梅州做网站百度热议

梅州做网站,百度热议,用例图在线制作网站,网站建设人员组织1.开发背景 Qt 开发过程中难免需要存储数据,可以选择保存到本地文件,但是查找比较麻烦,所以就有了数据库,主要是方便查找数据,增删改查等操作,而 SqLite 属于数据库中轻量级的存在,适合本地数据…

1.开发背景

        Qt 开发过程中难免需要存储数据,可以选择保存到本地文件,但是查找比较麻烦,所以就有了数据库,主要是方便查找数据,增删改查等操作,而 SqLite 属于数据库中轻量级的存在,适合本地数据存储功能。

2.开发需求

        Qt 界面上管理数据多个表的数据增删改查。

3.开发环境

        Window10 + Qt5.12.2 + QtCreator4.8.2

4.实现步骤

4.1 添加 SQL

        xxx.pro 文件添加 sql 模块

QT       += sql

4.2 软件编码

          主要头文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QTextEdit>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEtypedef struct
{quint32 num;QString name;QString data;}ItemData_t;class MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:private:Ui::MainWindow *ui;QSqlDatabase db;bool connectSQLite(QString name = "test.db");   // 连接数据库void disconnectSQLite(void);                    // 断开数据库bool addSQLiteForm(QString name);               // 增加表bool delSqLiteForm(QString name);               // 删除表void refreshFormShow(void);                     // 刷新表显示bool checkSQLiteItemExists(QString formName, quint32 num);bool addSQLiteItem(QString formName, const ItemData_t &item);           // 添加字段bool findSQLiteItem(QString formName, QString name, ItemData_t &item);  // 通过名字查找bool querySQLiteForm(QString formName);                                 // 查找整个数据表bool modifSQLiteItem(QString formName, QString name, QString dataNew);  // 修改数据库数据bool deleteSQLiteItem(QString formName, quint32 num);                   // 删除数据库字段
};#endif // MAINWINDOW_H

        主要源文件

#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QDebug>
#include <QInputDialog>
#include <QMessageBox>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);/* 连接信号和槽 */connect(ui->pushButton_Open, &QPushButton::clicked, [this](){if (db.isOpen()){QMessageBox::information(this, "提示", "数据库已打开");return;}connectSQLite();});connect(ui->pushButton_AddForm, &QPushButton::clicked, [this](){QString formName = QInputDialog::getText(this, "输入", "请输入表名");addSQLiteForm(formName);});connect(ui->pushButton_DelForm, &QPushButton::clicked, [this](){QString formName = ui->comboBox_Form->currentText();if (formName == nullptr){QMessageBox::information(this, "提示", "数据表空");return;}delSqLiteForm(formName);});connect(ui->pushButton_Query, &QPushButton::clicked, [this](){if (!db.isOpen()){QMessageBox::information(this, "提示", "数据库未打开");return;}QString formName = ui->comboBox_Form->currentText();querySQLiteForm(formName);});connect(ui->pushButton_Insert, &QPushButton::clicked, [this](){if (!db.isOpen()){QMessageBox::information(this, "提示", "数据库未打开");return;}/* 添加数据 */QString formName = ui->comboBox_Form->currentText();ItemData_t item;item.num = ui->lineEdit_Number->text().toUInt();item.name = ui->lineEdit_Name->text();item.data = ui->lineEdit_Data->text();addSQLiteItem(formName, item);});connect(ui->pushButton_Find, &QPushButton::clicked, [this](){QString formName = ui->comboBox_Form->currentText();ItemData_t item;item.name = ui->lineEdit_Name->text();findSQLiteItem(formName, item.name, item);});connect(ui->pushButton_Modif, &QPushButton::clicked, [this](){QString formName = ui->comboBox_Form->currentText();QString name = ui->lineEdit_Name->text();QString dataNew = ui->lineEdit_Data->text();modifSQLiteItem(formName, name, dataNew);});connect(ui->pushButton_delete, &QPushButton::clicked, [this](){QString formName = ui->comboBox_Form->currentText();quint32 num = ui->lineEdit_Number->text().toUInt();deleteSQLiteItem(formName, num);});/* 打开即连接 */connectSQLite();
}MainWindow::~MainWindow()
{disconnectSQLite();delete ui;
}/* 连接数据库 */
bool MainWindow::connectSQLite(QString name)
{/* 创建 SQLITE 数据库连接 */db = QSqlDatabase::addDatabase("QSQLITE");db.setDatabaseName(name);/* 打开数据库 */if (!db.open()){QMessageBox::critical(this, "Database Error", db.lastError().text());return false;}/* 刷新表格显示 */refreshFormShow();/* 返回成功 */return true;
}/* 断开数据库连接 */
void MainWindow::disconnectSQLite(void)
{/* 关闭数据库连接 */if (db.isOpen()){db.close();}
}/* 添加数据表 */
bool MainWindow::addSQLiteForm(QString name)
{if (!db.isOpen()){return false;}QString createTableQuery = QString("CREATE TABLE IF NOT EXISTS %1 (id INTEGER PRIMARY KEY, num INTEGER, name TEXT, data TEXT)").arg(name);QSqlQuery query;if (!query.exec(createTableQuery)){return false;}/* 创建成功 刷新显示 */refreshFormShow();return true;
}/* 删除表 */
bool MainWindow::delSqLiteForm(QString name)
{if (!db.isOpen()){return false;}QSqlQuery query;QString deleteTableQuery = QString("DROP TABLE IF EXISTS %1").arg(name);if (!query.exec(deleteTableQuery)){return false;}refreshFormShow();return true;
}/* 刷新表显示 */
void MainWindow::refreshFormShow(void)
{/* 查询数据库所有表 */QSqlQuery query;if (!query.exec("SELECT name FROM sqlite_master WHERE type='table';")){return;}/* 显示存在的表 */ui->comboBox_Form->clear();while (query.next()){QString tableName = query.value(0).toString();ui->comboBox_Form->addItem(tableName);}
}/* 查找是否存在 */
bool MainWindow::checkSQLiteItemExists(QString formName, quint32 num)
{QString checkQuery = QString("SELECT COUNT(*) FROM %1 WHERE num = :num").arg(formName);QSqlQuery query;if (!query.prepare(checkQuery)){return false;}query.bindValue(":num", num);if (query.exec() && query.next()){int count = query.value(0).toInt();if (count > 0){return true;}}return false;
}/* 添加数据 */
bool MainWindow::addSQLiteItem(QString formName, const ItemData_t &item)
{if (checkSQLiteItemExists(formName, item.num)){return false;}/* 选择数据表 */QString insertDataQuery = QString("INSERT INTO %1 (num, name, data) VALUES (:num, :name, :data)").arg(formName);QSqlQuery query;if (!query.prepare(insertDataQuery)){return false;}query.bindValue(":num", item.num);query.bindValue(":name", item.name);query.bindValue(":data", item.data);if (!query.exec()){return false;}ui->lineEdit_Number->clear();ui->lineEdit_Name->clear();ui->lineEdit_Data->clear();return true;
}/* 获取数据 数据 */
bool MainWindow::findSQLiteItem(QString formName, QString name, ItemData_t &item)
{if (!db.isOpen()){return false;}QString findDataQuery = QString("SELECT * FROM %1 WHERE name = :name").arg(formName);QSqlQuery query;if (!query.prepare(findDataQuery)){return false;}query.bindValue(":name", name);if (!query.exec()){return false;}QString result;while (query.next()){result += QString("ID: %1, Num: %2, Name: %3, Data: %4\n").arg(query.value(0).toInt()).arg(query.value(1).toInt()).arg(query.value(2).toString()).arg(query.value(3).toString());}ui->textEdit_Query->setText(result);return true;
}/* 显示数据库数据 */
bool MainWindow::querySQLiteForm(QString formName)
{if (!db.isOpen()){return false;}/* 选择表 */QString queryDataQuery = QString("SELECT * FROM %1").arg(formName);QSqlQuery query;if (!query.exec(queryDataQuery)){return false;}QString result;while (query.next()){result += QString("ID: %1, Num: %2, Name: %3, Data: %4\n").arg(query.value(0).toInt()).arg(query.value(1).toInt()).arg(query.value(2).toString()).arg(query.value(3).toString());}ui->textEdit_Query->setText(result);return true;
}/* 修改数据 */
bool MainWindow::modifSQLiteItem(QString formName, QString name, QString dataNew)
{if (!db.isOpen()){return false;}QString updateDataQuery = QString("UPDATE %1 SET data = :newData WHERE name = :name").arg(formName);QSqlQuery query;if (!query.prepare(updateDataQuery)){return false;}query.bindValue(":newData", dataNew);query.bindValue(":name", name);if (!query.exec()){return false;}return true;
}/* 通过编号删除数据 */
bool MainWindow::deleteSQLiteItem(QString formName, quint32 num)
{if (!db.isOpen()){return false;}QString updateDataQuery = QString("DELETE FROM %1 WHERE num = :num").arg(formName);QSqlQuery query;if (!query.prepare(updateDataQuery)){return false;}// 执行查询query.bindValue(":num", num);if (!query.exec()){return false;}return true;
}

4.3 运行测试

        可以实现基本的增删改查,还可以,数据库的操作需要熟悉数据库语句

4.4 第三方工具 HeidiSQL

        主要是可以查看数据库的实际情况,使用方法如下,可以看到写入的字段

http://www.shuangfujiaoyu.com/news/34411.html

相关文章:

  • 山东建设住建厅网站快速排名方案
  • 哪一个做网站模版好用的抖音seo排名软件
  • 怎么做网络销售的网站企业建站公司
  • 班级做网站人的叫什么广告优化师工资一般多少
  • 网站百度权重没有数据域名seo站长工具
  • 徐州云龙区建设局网站项目营销推广方案
  • 买域名做网站网络营销五个主要手段
  • 公众号登录手机版南京seo整站优化技术
  • 十大旅游网站排名百度网址大全网址导航
  • 工业设计appseo页面优化公司
  • 如何查询网站哪个公司做的傻瓜式自助建站系统
  • wordpress做双语网站百度云搜索引擎官网
  • 好看的移动端网站seo外包公司如何优化
  • 深圳做网站联系电话沈阳专业网站seo推广
  • 电子商务平台方案优化工具箱
  • 做免费的网站教程最近的新闻大事20条
  • 做视频哪个网站收入高搜索引擎查询
  • 网站制作费优化网站平台
  • 爬虫代理ip购买欧美seo查询
  • 怎么自己做礼品网站公司seo
  • 用新华做网站名是否侵权网络营销经典失败案例
  • 手机网站怎么搜索引擎设计网站大全
  • wordpress聊天长春网站优化咨询
  • 专门做情侣装的网站网上做广告怎么收费
  • 当地自己的淘宝网站怎么做关键词在线下载
  • 免费网页空间2023北京seo费用是多少
  • 模板网站制作公司今天的新闻大事10条
  • wordpress不间断音乐网站排名优化怎样做
  • 郑州制作网站哪家好怎样把产品放到网上销售
  • 精品课网站建设国产免费crm系统有哪些