1.關(guān)于查詢
模糊查詢查詢可以用like或glob,具體用法
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=[]}