网站的收录英语复试口语模板
- 作者: 五速梦信息网
- 时间: 2026年04月20日 08:10
当前位置: 首页 > news >正文
网站的收录,英语复试口语模板,南昌企业网站建设公司哪个好,石家庄网站制作模板1.开发背景 Qt 开发过程中难免需要存储数据#xff0c;可以选择保存到本地文件#xff0c;但是查找比较麻烦#xff0c;所以就有了数据库#xff0c;主要是方便查找数据#xff0c;增删改查等操作#xff0c;而 SqLite 属于数据库中轻量级的存在#xff0c;适合本地数据…1.开发背景 Qt 开发过程中难免需要存储数据可以选择保存到本地文件但是查找比较麻烦所以就有了数据库主要是方便查找数据增删改查等操作而 SqLite 属于数据库中轻量级的存在适合本地数据存储功能。 2.开发需求 Qt 界面上管理数据多个表的数据增删改查。 3.开发环境 Window10 Qt5.12.2 QtCreator4.8.2 4.实现步骤 4.1 添加 SQL xxx.pro 文件添加 sql 模块 QT sql4.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 QFormLayoutQT_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 QMessageBoxMainWindow::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 typetable;)){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 主要是可以查看数据库的实际情况使用方法如下可以看到写入的字段
- 上一篇: 网站的收录wordpress是cms
- 下一篇: 网站的数据库丢失武夷山网站设计
相关文章
-
网站的收录wordpress是cms
网站的收录wordpress是cms
- 技术栈
- 2026年04月20日
-
网站的实现怎么写网页源代码怎么查找部分内容
网站的实现怎么写网页源代码怎么查找部分内容
- 技术栈
- 2026年04月20日
-
网站的时间对齐应该怎么做怎样在网上做广告
网站的时间对齐应该怎么做怎样在网上做广告
- 技术栈
- 2026年04月20日
-
网站的数据库丢失武夷山网站设计
网站的数据库丢失武夷山网站设计
- 技术栈
- 2026年04月20日
-
网站的搜索引擎网页qq邮箱登录
网站的搜索引擎网页qq邮箱登录
- 技术栈
- 2026年04月20日
-
网站的特点有那些商业网站规划
网站的特点有那些商业网站规划
- 技术栈
- 2026年04月20日






