当前位置:首页 > 安卓源码 > 技术博客 >

Room使用过程中遇到的问题记录

时间:2019-08-07 14:48 来源:互联网 作者:源码搜藏 浏览: 收藏 挑错 推荐 打印

1.关于查询 模糊查询查询可以用 like 或 glob ,具体用法 LIKE LIKE用来匹配通配符指定模式的文本值。如果搜索表达式与模式表达式匹配,LIKE 运算符将返回真(true),也就是 1。这里有两个通配符与 LIKE 运算符一起使用,百分号(%)代表零个、一个或多个数字

1.关于查询

模糊查询查询可以用likeglob,具体用法

LIKE

LIKE用来匹配通配符指定模式的文本值。如果搜索表达式与模式表达式匹配,LIKE 运算符将返回真(true),也就是 1。这里有两个通配符与 LIKE 运算符一起使用,百分号(%)代表零个、一个或多个数字或字符。下划线(_)代表一个单一的数字或字符。这些符号可以被组合使用。

1、查找字段A以AAA开头的任意值
  select * from table_name where 字段A like 'AAA%'
2、查找字段A任意位置包含AAA的任意值
  select * from table_name where 字段A like '%AAA%'
3、查找字段A第二位和第三位为 AA 的任意值
  select *from table_name where 字段A like '_AA%'
4、查找字段A以 A 开头,且长度至少为 3 个字符的任意值
  select * from table_name where 字段A like 'A_%_%'
5、查找字段A以 A 结尾的任意值
  select *from table_name where 字段A like '%A'
6、查找字段A第二位为 A,且以 B 结尾的任意值
  select *from table_name where 字段A like '_A%B'
7、查找字段A长度为 5 位数,且以 A 开头以 B 结尾的任意值(A,B中间三个下划线)
  select *from table_name where 字段A like 'A___B'

GLOB

SQLite 的 GLOB 运算符是用来匹配通配符指定模式的文本值。如果搜索表达式与模式表达式匹配,GLOB 运算符将返回真(true),也就是 1。与 LIKE 运算符不同的是,GLOB 是大小写敏感的,对于下面的通配符,它遵循 UNIX 的语法。
星号(*)代表零个、一个或多个数字或字符。问号(?)代表一个单一的数字或字符。这些符号可以被组合使用。
1、查找字段A以AAA开头的任意值
  select * from table_name where 字段A GLOB 'AAA*'
2、查找字段A任意位置包含AAA的任意值
  select * from table_name where 字段A GLOB '*AAA*'
3、查找字段A第二位和第三位为 AA 的任意值
  select *from table_name where 字段A GLOB '?AA*'
4、查找字段A以 A 开头,且长度至少为 3 个字符的任意值
  select * from table_name where 字段A GLOB 'A?*?*'
5、查找字段A以 A 结尾的任意值
  select *from table_name where 字段A GLOB '*A'
6、查找字段A第二位为 A,且以 B 结尾的任意值
  select *from table_name where 字段A GLOB '?A*B'
7、查找字段A长度为 5 位数,且以 A 开头以 B 结尾的任意值(A,B中间三个下划线)
  select *from table_name where 字段A GLOB 'A???B'

在room中,真接写 LIKE '% :key %'或者 "LIKE '%"+:key+“% '"都有问题,正确写法如下:
@Query("SELECT * FROM tb_use WHERE Name LIKE '%' || :name || '%')
参考

2.Room升级问题

androidx.room 数据库升级Migration ,创建表及更改表时,字段为Integer(包括boolean)需设置NOT NULL
不然报 :java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/com.xxxx/databases/.....

java.lang.IllegalStateException: Migration didn't properly handle ChatRecordSchedule(xxx.model.ChatRecordSchedule).
TableInfo{name='ChatRecordSchedule', columns={wechatAccountId=Column{name='wechatAccountId', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}, chatId=Column{name='chatId', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=2}, last=Column{name='last', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, bottom=Column{name='bottom', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, from=Column{name='from', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, to=Column{name='to', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, state=Column{name='state', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=3}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='ChatRecordSchedule', columns={wechatAccountId=Column{name='wechatAccountId', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}, chatId=Column{name='chatId', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=2}, last=Column{name='last', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, bottom=Column{name='bottom', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, from=Column{name='from', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, to=Column{name='to', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, state=Column{name='state', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, type=Column{name='type', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=3}}, foreignKeys=[], indices=[]}

 

Room使用过程中遇到的问题记录 转载https://www.codesocang.com/appboke/41789.html

技术博客阅读排行

最新文章