Room使用過程中遇到的問題記錄

1.關(guān)于查詢

模糊查詢查詢可以用likeglob,具體用法

LIKE

LIKE用來匹配通配符指定模式的文本值。如果搜索表達(dá)式與模式表達(dá)式匹配,LIKE 運算符將返回真(true),也就是 1。這里有兩個通配符與 LIKE 運算符一起使用,百分號(%)代表零個、一個或多個數(shù)字或字符。下劃線(_)代表一個單一的數(shù)字或字符。這些符號可以被組合使用。

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 結(jié)尾的任意值
  select *from table_name where 字段A like '%A'
6、查找字段A第二位為 A,且以 B 結(jié)尾的任意值
  select *from table_name where 字段A like '_A%B'
7、查找字段A長度為 5 位數(shù),且以 A 開頭以 B 結(jié)尾的任意值(A,B中間三個下劃線)
  select *from table_name where 字段A like 'A___B'

GLOB

SQLite 的 GLOB 運算符是用來匹配通配符指定模式的文本值。如果搜索表達(dá)式與模式表達(dá)式匹配,GLOB 運算符將返回真(true),也就是 1。與 LIKE 運算符不同的是,GLOB 是大小寫敏感的,對于下面的通配符,它遵循 UNIX 的語法。
星號(*)代表零個、一個或多個數(shù)字或字符。問號(?)代表一個單一的數(shù)字或字符。這些符號可以被組合使用。
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 結(jié)尾的任意值
  select *from table_name where 字段A GLOB '*A'
6、查找字段A第二位為 A,且以 B 結(jié)尾的任意值
  select *from table_name where 字段A GLOB '?A*B'
7、查找字段A長度為 5 位數(shù),且以 A 開頭以 B 結(jié)尾的任意值(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 數(shù)據(jù)庫升級Migration ,創(chuàng)建表及更改表時,字段為Integer(包括boolean)需設(shè)置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=[]}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容