Android Room 数据库的坑

1.Android Room 数据库的坑

在用Room数据库的时候 发现有需要一个字段的条件合到一起去写这个SQL

java 复制代码
 @Query("SELECT * FROM 表名 WHERE 字段A = '1' and 字段B <= :Time " +
            "and 字段B >= :Time and 字段C <= :BTime and 字段C >= :BTime " +
            "and '(' || 字段D is null or 字段D = '' || ')'")
    List selectList(String Time, String BTime);

这里面的 " ||" 是Room里面独特的表达方式 是替代了java里面的"+"号

正常在android中 使用 是这样的

java 复制代码
String sql = "SELECT * FROM 表名 WHERE 字段A = '1' and 字段B <= "+传入的参数+" " +
            "and 字段B >= "+传入的参数+" and 字段C <= "+传入的参数+" and 字段c >= "+传入的参数+" " +
            "and '(' "+" 字段D is null or 字段D = '' "+" ')'"


cursor = db.rawQuery(sql, null);

而在Room 中 用 "||" 代替了 "+"

2.Android Room 查询语句的坑

less 复制代码
@Query("SELECT * FROM 表名 WHERE 字段A = '0' order by id desc")
List selectList();

假如你正在查询一张表的面的内容,然后忽然跑出来一个异常

less 复制代码
# [Android RoomDatabase Cruash "Cursor window allocation of 4194304 bytes failed"](https://stackoverflow.com/questions/75456123/android-roomdatabase-cruash-cursor-window-allocation-of-4194304-bytes-failed)

奔溃日志:

css 复制代码
android.database.CursorWindowAllocationException: Could not allocate CursorWindow '/data/user/0/cn.xxx.xxx/databases/xxx.db' of size 2097152 due to error -13.
	at android.database.CursorWindow.nativeCreate(Native Method)
	at android.database.CursorWindow.<init>(CursorWindow.java:139)
	at android.database.CursorWindow.<init>(CursorWindow.java:120)
	at android.database.AbstractWindowedCursor.clearOrCreateWindow(AbstractWindowedCursor.java:202)
	at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:147)
	at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:140)
	at yd.d.m(SourceFile:21)
	at cn.xxx.control.y.y0(SourceFile:1)
	at e5.y.p(SourceFile:230)
	at e5.y.l(SourceFile:1)
	at e5.y.E(SourceFile:1)
	at cn.xxx.cluster.classin.viewmodel.SessionViewModel$d.invokeSuspend(SourceFile:42)

触发原因

  • Room 对应的 Sqlite 数据库,其对 CursorWindows 分配的大小是有限制的,最大为 2M,超过之后会发生上述崩溃闪退现象(偶现且难以复现的 bug

解决方法

需要业务方梳理这块的业务,优化数据库的调用,如果明确知道在一个方法里面会调用多个数据库的方法,需要让 controller 提供新的方法,且这个 controller 层的方法需要添加 @Transaction 进行注解,从而保证在同一个事物内进行数据库操作,以此避免 CursorWindows 大小超过 2M

那么问题来了 @Transaction 这个注解是干嘛的呢

翻译 事务的意思

less 复制代码
@Transaction
@Query("SELECT * FROM 表名 WHERE 字段A = '0' order by id desc")
List selectList();

接着 问题完美解决

大家学费了嘛 学费的评论区Q1 没学费的抠眼珠子

相关推荐
雪球Snowball1 天前
【Android关键流程】Configuration变更时更新应用程序配置
android
h7ml1 天前
于 CompletableFuture 的异步编排优化企业微信通知发送性能
android·windows·企业微信
子木鑫1 天前
[SUCTF 2019] CheckIn1 — 利用 .user.ini 与图片马构造 PHP 后门并绕过上传检测
android·开发语言·安全·php
风清云淡_A1 天前
【ANDROID】使用webview实现加载第三方的网页效果
android
吴声子夜歌1 天前
RxJava——操作符详解(四)
android·echarts·rxjava
我是阿亮啊1 天前
Android Handler 消息机制之 Looper 深度解析
android·loop·handler·looper
Mr YiRan1 天前
Android 16KB 腾讯Mars XLog适配
android
2501_915921431 天前
不用 Xcode 上架 iOS,拆分流程多工具协作完成 iOS 应用的发布准备与提交流程
android·macos·ios·小程序·uni-app·iphone·xcode
子木鑫1 天前
[SUCTF2019 & GXYCTF2019] 文件上传绕过实战:图片马 + .user.ini / .htaccess 构造 PHP 后门
android·开发语言·安全·php
一起养小猫1 天前
Flutter for OpenHarmony 实战:打造功能完整的记账助手应用
android·前端·flutter·游戏·harmonyos