数据库就是保存数据的文件。可以存储大量数据,包括插入数据、更新数据、截取数据等。用专业术语来说,数据库是“按照数据结构来组织、存储和管理数据的仓库”。
什么时候需要数据库?在嵌入式里,存储大量数据,或者记录数据,就需要用到数据库。比如手机的闹钟就使用到了数据库,我们设置的闹钟数据将会保存到数据库里,闹钟程序运行时会从数据库里读取出上次保存的闹钟数据。如果没有数据库,则闹钟程序关机了数据不保存在物理储存设备里,下次运行闹钟时就没有上次设置的闹钟数据,这显然是不合理的。所以我们需要用到数据库。想要在项目中使用Qt SQL模块,需要在项目配置文件里添加QT += core gui sql
Qt SQL模块为数据库提供了编程支持,Qt支持很多种常见的数据库,如MySQL、Oracle、MS SQL Server、SQLite等。Qt SQL模块里包含了很多个类,可以轻松实现数据库的连接、执行SQL语句,获取数据库里的数据与界面显示等功能,一般数据与界面之间会采用Model/View架构,很方便的显示数据界面和操作数据库。在嵌入式里,一般常用的数据库就是Sqlite3。SQLite是非常小轻量级的,完全配置时小于400KB,省略可选功能配置时小于250KB。SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的SQL数据库引擎。它是一个零配置的数据库,这意味着与其他数据库不一样,您不需要在系统中配置。就像其他数据库SQLite引擎不是一个独立的进程,可以按应用程序需求进行静态或动态连接。SQLite可以直接访问其存储文件。
.pro文件
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
numberpicker.cpp \
switchbutton.cpp
HEADERS += \
mainwindow.h \
numberpicker.h \
switchbutton.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
res.qrc
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
/* 指定文件 */
QFile file(":/style.qss");
/* 判断文件是否存在 */
if (file.exists() )
{
/* 以只读的方式打开 */
file.open(QFile::ReadOnly);
/* 以字符串的方式保存读出的结果 */
QString styleSheet = QLatin1String(file.readAll());
/* 设置全局样式 */
qApp->setStyleSheet(styleSheet);
/* 关闭文件 */
file.close();
}
MainWindow w;
w.show();
return a.exec();
}
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMainWindow>
#include <QDialog>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QListWidget>
#include <QLabel>
#include <QTime>
#include <QSqlTableModel>
#include "numberpicker.h"
#include "switchbutton.h"
class NumberPicker;
class SwitchButton;
/* ListWiget项结构体 */
struct ItemObjectInfo {
/* 闹钟开关 */
SwitchButton *switchButton;
/* Widget容器 */
QWidget *widget;
/* 水平布局 */
QHBoxLayout *hBoxLayout;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
/* 数据库连接类 */
QSqlDatabase sqlDatabase;
/* 数据库操作模型 */
QSqlTableModel *model;
/* 时针选择器 */
NumberPicker *hourPicker;
/* 分钟选择器 */
NumberPicker *minutePicker;
/* 弹出选择时间对话框 */
QDialog *alarmDialog;
/* 水平布局 */
QHBoxLayout *hBoxLayout[3];
/* 垂直布局 */
QVBoxLayout *vBoxLayout[2];
/* 显示闹钟列表 */
QListWidget *listWidget;
/* 主Widget */
QWidget *mainWidget;
/* 底部Wiget */
QWidget *bottomWidget;
/* 弹出对话框布局窗口选择时间容器 */
QWidget *timeWidget;
/* 弹出对话框布局窗口按钮容器 */
QWidget *btWidget;
/* 添加闹钟按钮 */
QPushButton *addAlarm;
/* 确认按钮 */
QPushButton *yesButton;
/* 取消按钮 */
QPushButton *cancelButton;
/* listWiget项信息存储 */
QVector<ItemObjectInfo> itemObjectInfo;
private slots:
/* 添加闹钟按钮被点击 */
void addAlarmClicked();
/* 列表被点击 */
void listWidgetItemClicked(QListWidgetItem *);
/* 确认按钮被点击 */
void yesButtonClicked();
/* 取消按钮被点击 */
void cancelButtonClicked();
/* 开关按钮点击 */
void switchButtonClicked(bool);
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include <QDebug>
#include <QSqlError>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
/* 设置主窗体的显示位置与大小 */
this->setGeometry(0, 0, 800, 480);
/* 查看本机可用的数据库驱动 */
QStringList drivers = QSqlDatabase::drivers();
foreach(QString driver, drivers) {
qDebug()<<driver;
}
/* 以QSQ