Qt sqlite操作笔记

⌚Time: 2022-06-09 20:42:31

👨‍💻Author: Jack Ge

返回日期2010-10-01 00:00:00到2023-06-09 01:23:00之间的所有记录,注意sql中日期数据不能写成2010-1-1 0:0:0这种,因此在编程时要对执行语句进行格式化


select * from table_1 where 日期 between datetime('2010-10-01 00:00:00') and datetime('2023-06-09 01:23:00')

对于使用时间作为日期列默认值,使用


日期 timestamp not null default(datetime())


        query.exec("create table table_1 ("

                   "id integer primary key,"

                   "用户 text,"

                   "日期 timestamp not null default(datetime()),"

                   "`角度(mil)` text,"

                   "范围 text default \"-250~+250\","

                   "故障 bool)"

                   );

对于QSqlQuery使用查询语句,若要查询的列名有中文或者括号,需要用

``

将它包括起来,并且执行完后,需要调用next方法来取得数据


query.exec("select * from table_1 where `测试元件` = \"传感器\"");

qDebug()<<query.lastError();

//获得“检测值”列的索引号

int index = query.record().indexOf("检测值");

//取得数据

query.next();

//取得“测试元件”列是“传感器”的一条记录,并且获取其“检测值”列的内容

QString value = query.value(index).toString();

返回刚插入的数据的id primaru key


select * from table_1 where _id = last_insert_rowid()

查询第22条记录开始的共5条记录


select * from table_1 limit 22,5

查询日期最小的记录


select * from table_0 order by date asc limit 0,1;

查询日期最大的记录


select * from table_0 order by date desc limit 0,1;