QT Sqlite 内存模式 简单读写

//本文描述了QT Sqlite 内存模式 ,使用QT 自带库文件,写入和读取。

//QT 6.2.4 MSVC2019调试通过。

//需要在pro文件中加入QT += sql

#include <QCoreApplication>

#include <QSqlDatabase>

#include <QSqlQuery>

#include <QDebug>

#include <QSqlDriver>

//#include <QSqlError>

//#include <QStringList>

//#include <QVariant>

static bool createConnection()

{

//addDatabase的原型是

//QSqlDatabase::addDatabase

// (const QString &type,/*类型*/

// const QString &connectionName = QLatin1String(defaultConnection))//连接名称

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE" );

db.setDatabaseName(":memory:" );

if(!db.open() )

{

qDebug()<<"sqlite memory db is not open";

return false;

}

QSqlQuery query;

query.exec("create table student(id int primary key,name vachar(20))");//

query.exec("insert into student values (0,'Jack_Ma')");

query.exec("insert into student values(1,'HuaTeng_Ma')");

query.exec("insert into student values(2,'QiangDong_Liu')");

query.exec("insert into student values(3,'YanHong_Li')");

query.exec("insert into student values(4,'Lei_Ding')");

return true;

}

int main(int argc,char* argv\[\])

{

QCoreApplication a(argc, argv);

qDebug()<<"Available drivers:";

QStringList dataDrivers = QSqlDatabase::drivers() ;

for(auto item:dataDrivers)

{

qDebug()<<item;

}

if(!createConnection() )

return 1;

QSqlQuery query;

query.exec("select * from student");

qDebug()<<"all records:";

while(query.next() )

{

qDebug()<<query.value (0).toInt()<<query.value (1).toString();//逐行打印各记录

}

query.exec("select count (*) from student");

qDebug()<<"records count number:";

while(query.next() )

{

qDebug()<<query.value (0).toInt();//打印记录数

}

return a.exec();

}

相关推荐
用户805533698032 天前
不止三件套:QObject 属性系统全关键字与运行时反射!
c++·qt
xcyxiner2 天前
DicomViewer (vcpkg Windows和ubuntu编译)7
qt
Quz7 天前
QML Hello World 入门示例
qt
兵慌码乱8 天前
面向桌面端的资产管理系统分层架构设计与核心模块实现
python·系统架构·sqlite·pyqt5·数据库设计·桌面应用开发·mvc架构
兵慌码乱9 天前
基于Python+PyQt5+SQLite的药房管理系统实现:事务一致性与界面解耦全流程解析
python·sqlite·信号与槽·pyqt5·数据库设计·桌面应用开发·事务处理
xcyxiner10 天前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner11 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner11 天前
DicomViewer (添加模型类)3
qt
xcyxiner12 天前
DicomViewer (目录调整) 2
qt
xcyxiner12 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt