前言
C++QT SqlLite数据库简单使用
一、数据库简单使用
语言:C++
框架:Qt
数据库:Sqllite
设计模式:单例
二、代码
//Database.h #pragma once #include <QObject> #include <QSqlQuery> #include <QDebug> #include <QSqlError> #include <QMutex> #include <QDir> #include <QCoreApplication> #include <QDate> #include <QDateTime> struct InfoEquipPatient { QString Hospital; QString Doctor; QString PatientId; QString PatientName; QString PatientSex; QString PatientAge; QString Disease; QString Location; QString Time; QString CodeEquip; }; class Database : public QObject { Q_OBJECT public: static Database* instance() { static QMutex mutex; if (!m_instance) { QMutexLocker locker(&mutex); if (!m_instance) { m_instance = new Database(); } } return m_instance; } static void destroy() { if (m_instance) delete m_instance; } explicit Database(QObject *parent = 0); ~Database(); //建立数据库连接 bool CreateConnect(); //关闭连接 void CloseConnect(); //创建表 void CreateTable(); //获取所有设备手术信息 void GetAllInfoEquips(QVector<InfoEquipPatient> &infos); QSqlDatabase m_dbconn; private: static Database* m_instance; };
//Database.cpp #include "Database.h" Database *Database::m_instance = 0; Database::Database(QObject *parent) : QObject(parent) { } Database::~Database() { } bool Database::CreateConnect() { m_dbconn = QSqlDatabase::addDatabase("QSQLITE", "MyConnect"); QString fullpath = "spine.db";//数据库文件在debug/release根目录下,一般会自己创建 m_dbconn.setDatabaseName(fullpath); //数据库登录用户名和密码设置 m_dbconn.setUserName("root"); m_dbconn.setPassword("123456"); if (!m_dbconn.open()) { qDebug() << "Database::CreateConnect()::failed connect."; return false; } else { qDebug() << "Database::CreateConnect()::success connect."; CreateTable(); return true; } } void Database::CloseConnect() { m_dbconn.close(); } void Database::CreateTable() { QSqlQuery query(this->m_dbconn); QString str_SqlEquipInfo = "CREATE TABLE InfoEquip(Hospital varchar(1024),Doctor varchar(1024),PatientId varchar(1024),PatientName varchar(1024),PatientSex varchar(10),PatientAge varchar(10),Disease varchar(1024),Location varchar(100),Time varchar(100),EquipCode varchar(100));"; if (query.exec(str_SqlEquipInfo)) { qDebug() << "Database::::Create Tables successful!"; } else { qDebug() << "Database::::Tables Exist!"; } } void Database::GetAllInfoEquips(QVector<InfoEquipPatient> &infos) { QSqlQuery query(this->m_dbconn); QString str_SqlSelectAll = "select * from InfoEquip"; query.prepare(str_SqlSelectAll); if (!query.exec()) { qDebug() << query.lastError(); } else { qDebug() << "Database::get_all_infoEquips!"; while (query.next()) { InfoEquipPatient info; info.Hospital = query.value("Hospital").toString(); info.Doctor = query.value("Doctor").toString(); info.PatientId = query.value("PatientId").toString(); info.PatientName = query.value("PatientName").toString(); info.PatientSex = query.value("PatientSex").toString(); info.PatientAge = query.value("PatientAge").toString(); info.Disease = query.value("Disease").toString(); info.Location = query.value("Location").toString(); info.Time = query.value("Time").toString(); info.CodeEquip = query.value("EquipCode").toString(); infos.push_back(info); } } }
int main(int argc, char *argv[]) { QApplication a(argc, argv); //创建数据库连接 Database *m_database = Database::instance(); m_database->CreateConnect(); return a.exec(); }
//XXX.h #include"Database.h" Database *m_database;//类内声明 //XXX.cpp调用数据库查询 m_database = Database::instance(); QVector<InfoEquipPatient> infos; m_database->GetAllInfoEquips(infos);
推荐一个零声学院项目课,个人觉得老师讲得不错,分享给大家:
零声白金学习卡(含基础架构/高性能存储/golang云原生/音视频/Linux内核)
https://xxetb.xet.tech/s/VsFMs