QT+SQLite
在QT中使用sqlite数据库,有多种使用方法,在这里我只提供几种简单,代码简短的方法,包括一些特殊字符处理。在这里也给大家说明一下,如果你每次要存储的数据量很大,建议使用事务(代码中有体现),万条数据不到一秒吧。
用SQlite建立一个简单学生管理数据库
数据库中有两个表一个是class和student。

class表结构

student表结果

创建工程
我的工程如下:

直接上代码(看注释更通透)
student.pro文件添加sql模块。
QT += core gui
QT += sql #添加数据库模块
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
Student.cpp
HEADERS += \
Student.h
FORMS += \
Student.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${
TARGET}/bin
else: unix:!android: target.path = /opt/$${
TARGET}/bin
!isEmpty(target.path): INSTALLS += target
student.h文件添加相关定义。
#ifndef STUDENT_H
#define STUDENT_H
#include <QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QDebug>
QT_BEGIN_NAMESPACE
namespace Ui {
class Student; }
QT_END_NAMESPACE
class Student :

本文介绍了在QT应用中使用SQLite数据库的基本操作,包括添加、删除、更新和查询数据,以及如何在处理大量数据时利用事务提高效率。
最低0.47元/天 解锁文章
2596

被折叠的 条评论
为什么被折叠?



