Android-sqlite的命令,保证数据库只有最新的若干条

2023-12-23 05:38

本文主要是介绍Android-sqlite的命令,保证数据库只有最新的若干条,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

创建表

//创建表create table student(sid integer primary key autoincrement not null,
gender varchar(20)  not null,
score  integer  not null);

insert into student(sname,gender,score) values('Li','male',88);CREATE TABLE Cars(Id integer PRIMARY KEY, Name text, Cost integer);
INSERT INTO Cars VALUES(1,'Audi',52642);

delete from student where sid=3;

update student set score=100,gender='male' where sname='Mike';

select * from student;
select * from student where gender='female';
//模糊查询
select * from student where sname like '%a%';
select * from student where sname like 'a%';
//排序查询
select * from student order by score desc;    降序
select * from student order by score;         默认升序
select * from student order by score asc;    升序

报错:

android.database.sqlite.SQLiteException: no such column: (Sqlite code 1)


AndroidRuntime: FATAL EXCEPTION: mainProcess: com.xq.mygreendao, PID: 1742android.database.sqlite.SQLiteException: no such column: 许强 (Sqlite code 1): , while compiling: insert into user(name,age,sex,salary) values(许强,18,男,160000), (OS error - 2:No such file or directory)at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:925)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:536)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:603)at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:63)at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1965)at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1890)at com.xq.mygreendao.db.helper.UserSqlUtils.insertSQL(UserSqlUtils.java:52)at com.xq.mygreendao.MainActivity.insertSql(MainActivity.java:256)at com.xq.mygreendao.MainActivity.onClick(MainActivity.java:125)at android.view.View.performClick(View.java:5647)at android.view.View$PerformClick.run(View.java:22479)at android.os.Handler.handleCallback(Handler.java:761)at android.os.Handler.dispatchMessage(Handler.java:98)at android.os.Looper.loop(Looper.java:156)at android.app.ActivityThread.main(ActivityThread.java:6623)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

检查建表字段的类型,然后打印输出的语句是否符合:

这里写图片描述

字段都为字符串。

 public void insertSQL(String name, String age, String sex, String salary) {SQLiteDatabase db = DBHelper.getInstance(context).getReadableDatabase();//String stu_sql="insert into user(name,age,sex,salary) values('许强','18','男','12700')";String strSql = "insert into " + UserTableUtil.TABLE_NAME +"(" + UserTableUtil.NAME + "," + UserTableUtil.AGE + "," + UserTableUtil.SEX + "," + UserTableUtil.SALARY + ")" +" values('" + name + "','" + age + "','" + sex + "','" + salary + ")";Log.e("UserSqlUtils", "插入数据====" + strSql);db.execSQL(strSql);}

打印log:

insert into user(name,age,sex,salary) values('许强','18','男','500000)

发现最后少了一个单引号

修改后:

 public void insertSQL(String name, String age, String sex, String salary) {SQLiteDatabase db = DBHelper.getInstance(context).getReadableDatabase();//String stu_sql="insert into user(name,age,sex,salary) values('许强','18','男','12700')";String strSql = "insert into " + UserTableUtil.TABLE_NAME +"(" + UserTableUtil.NAME + "," + UserTableUtil.AGE + "," + UserTableUtil.SEX + "," + UserTableUtil.SALARY + ")" +" values('" + name + "','" + age + "','" + sex + "','" + salary + "')";Log.e("UserSqlUtils", "插入数据====" + strSql);db.execSQL(strSql);}

再次运行打印log:

insert into user(name,age,sex,salary) values('许强','18','男','500000')

保证数据库只有最新的5条数据

drop table if exists student;create table student
(sid    integer primary key autoincrement not null,sname  varchar(20)                       not null,gender varchar(20)                       not null,score  integer                           not null
);insert into student(sname, gender, score)
values ('Li1', 'male', 88);
insert into student(sname, gender, score)
values ('Li2', 'male', 88);
insert into student(sname, gender, score)
values ('Li3', 'male', 88);
insert into student(sname, gender, score)
values ('Li4', 'male', 88);
insert into student(sname, gender, score)
values ('Li5', 'male', 88);select count(*)
from student;insert into student(sname, gender, score)
values ('Li6', 'male', 88);
insert into student(sname, gender, score)
values ('Li7', 'male', 88);-- 查询表中总共多少条数据
select count(*)
from student;-- 按sid倒叙查询第六条数据的sid
select sid
from student
order by sid desc
limit 1 offset 5;-- 删除表中sid小于等于第六条sid的数据( 核心 )
delete
from student
where sid <= (select sidfrom studentorder by sid desclimit 1 offset 5);

其他

SELECT * FROM COMPANY LIMIT 3 OFFSET 2;
//offset 2:偏移两条数据,从第三个开始
//limit 3 :取出三条数据

这篇关于Android-sqlite的命令,保证数据库只有最新的若干条的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/526904

相关文章

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

Linux如何查看文件权限的命令

《Linux如何查看文件权限的命令》Linux中使用ls-R命令递归查看指定目录及子目录下所有文件和文件夹的权限信息,以列表形式展示权限位、所有者、组等详细内容... 目录linux China编程查看文件权限命令输出结果示例这里是查看tomcat文件夹总结Linux 查看文件权限命令ls -l 文件或文件夹

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

idea的终端(Terminal)cmd的命令换成linux的命令详解

《idea的终端(Terminal)cmd的命令换成linux的命令详解》本文介绍IDEA配置Git的步骤:安装Git、修改终端设置并重启IDEA,强调顺序,作为个人经验分享,希望提供参考并支持脚本之... 目录一编程、设置前二、前置条件三、android设置四、设置后总结一、php设置前二、前置条件

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

C语言进阶(预处理命令详解)

《C语言进阶(预处理命令详解)》文章讲解了宏定义规范、头文件包含方式及条件编译应用,强调带参宏需加括号避免计算错误,头文件应声明函数原型以便主函数调用,条件编译通过宏定义控制代码编译,适用于测试与模块... 目录1.宏定义1.1不带参宏1.2带参宏2.头文件的包含2.1头文件中的内容2.2工程结构3.条件编

虚拟机Centos7安装MySQL数据库实践

《虚拟机Centos7安装MySQL数据库实践》用户分享在虚拟机安装MySQL的全过程及常见问题解决方案,包括处理GPG密钥、修改密码策略、配置远程访问权限及防火墙设置,最终通过关闭防火墙和停止Net... 目录安装mysql数据库下载wget命令下载MySQL安装包安装MySQL安装MySQL服务安装完成

MySQL进行数据库审计的详细步骤和示例代码

《MySQL进行数据库审计的详细步骤和示例代码》数据库审计通过触发器、内置功能及第三方工具记录和监控数据库活动,确保安全、完整与合规,Java代码实现自动化日志记录,整合分析系统提升监控效率,本文给大... 目录一、数据库审计的基本概念二、使用触发器进行数据库审计1. 创建审计表2. 创建触发器三、Java

MySQL 迁移至 Doris 最佳实践方案(最新整理)

《MySQL迁移至Doris最佳实践方案(最新整理)》本文将深入剖析三种经过实践验证的MySQL迁移至Doris的最佳方案,涵盖全量迁移、增量同步、混合迁移以及基于CDC(ChangeData... 目录一、China编程JDBC Catalog 联邦查询方案(适合跨库实时查询)1. 方案概述2. 环境要求3.