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();

}

相关推荐
行十万里人生5 小时前
Qt事件处理:理解处理器、过滤器与事件系统
开发语言·git·qt·华为od·华为·华为云·harmonyos
黑金IT6 小时前
Python3 + Qt5:实现AJAX异步更新UI
qt·ui·ajax
人工智能教学实践7 小时前
基于 yolov8_pyqt5 自适应界面设计的火灾检测系统 demo:毕业设计参考
qt·yolo·课程设计
扎量丙不要犟8 小时前
跨平台的客户端gui到底是选“原生”还是web
前端·javascript·c++·qt·rust·electron·tauri
Music 爱好者1 天前
DRF开发避坑指南01
数据库·python·django·sqlite
笑鸿的学习笔记1 天前
qt-Quick3D笔记之官方例程Runtimeloader Example运行笔记
笔记·qt·3d
菜一头包1 天前
线程池以及在QT中的接口使用
c++·qt
R三哥哥啊1 天前
【Qt5】声明之后快速跳转
开发语言·qt·qt5
深蓝海拓1 天前
使用QSqlQueryModel创建交替背景色的表格模型
数据库·qt·pyqt
冰激凌zz2 天前
QT TLS initialization failed
qt