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

}

相关推荐
程序leo源1 天前
Qt窗口详解
开发语言·数据库·c++·qt·青少年编程·c#
我在人间贩卖青春1 天前
重学Qt——事件处理
qt
o丁二黄o1 天前
长上下文工程实战:用Gemini镜像站构建跨文档关联分析与自动报告系统
sqlite
小宋0011 天前
QT中控件qss样式修改
开发语言·qt
m0_617493941 天前
PySide6 数据库操作深度实测:从 SQLite 连接到增删改查避坑指南
jvm·数据库·sqlite
csdn小瓯1 天前
PostgreSQL迁移实战:从SQLite到生产级数据库的平滑演进
数据库·postgresql·sqlite
yuechuji0011 天前
三、MPR(三平面重建)和三视图
qt
Hua-Jay2 天前
OpenCV联合C++/Qt 学习笔记(二十二)----相机模型与投影及单目相机标定
c++·笔记·qt·opencv·学习·计算机视觉
小短腿的代码世界2 天前
QCefView架构深度解析:从Chromium嵌入到Qt信号槽集成的完整技术链路
qt·架构
byxdaz2 天前
Qt修改操作系统的日期与时间
qt