我的Dao文件中包含以下代码。
@Query("SELECT * FROM ${Test.TABLE_NAME} WHERE primary_key = :primaryKey LIMIT 1")
fun selectTest(primaryKey: String): Test?
问题是primaryKey
是一个字符串,其中可以包含/
。如Test/4
。这将导致查询失败。我尝试在:primaryKey
周围加上引号,但是Room不再绑定它了。我还尝试将转义的引号添加到将绑定到查询的primaryKey
字符串中,但这也不起作用。
关于如何处理此问题的想法?
答案 0 :(得分:0)
表名包含特殊字符,因此必须用SQLite和ROOM可接受的字符括起来。
在SQLITE中,您可以使用以下之一:-
`tablename`
"tablename"
'tablename'
[tablename]
然而,ROOM却比较麻烦,因为它不允许您使用中间的两个。这样尝试:-
@Query("SELECT * FROM `${Test.TABLE_NAME}` WHERE primary_key = :primaryKey LIMIT 1")
fun selectTest(primaryKey: String): Test?
或
@Query("SELECT * FROM [${Test.TABLE_NAME}] WHERE primary_key = :primaryKey LIMIT 1")
fun selectTest(primaryKey: String): Test?
使用:-
@Query("SELECT * FROM `/cards` WHERE card_name LIKE :cardName")
Cards[] getCardsByName(String cardName);
@Query("SELECT * FROM [" + GamesDatabase.TABLENAMECARDS +"] WHERE card_name LIKE :cardName")
Cards[] getFunnyCardsByName(String cardName);
GamesDatabase.TABLENAMECARDS
是相同的表名,但为常量,即/ cards 以上道的用法是:-
String cardToGet = "%/";
Cards[] retrievedCards = mGamesDao.getCardsByName(cardToGet);
for (Cards c: retrievedCards) {
logCardsInfo(c,"GETCARDSBYNAME");
}
retrievedCards = mGamesDao.getFunnyCardsByName(cardToGet);
for (Cards c: retrievedCards) {
logCardsInfo(c,"GETCARDSBYFUNNYNAME");
}
logCardsInfo方法写入卡,它的名称,值,它的ID,它的文件夹和它的类别(作为要传递的第二个参数的TAG)。
其中Cards包含一包纸牌(带有一个称为ooops的额外花色,不适合等同于Category,每个Category的文件夹中有两个,但normalcards文件夹中有7个),但是卡的名称为7是7 /而不是7,所以按照:-
2019-09-16 15:01:46.084 19140-19140/aso.so57943963foreignkeys D/GETCARDSBYNAME: Card Name is 7/
Value is 7
CardID is 10
In Folder NormalCards
In Category Spades
2019-09-16 15:01:46.089 19140-19140/aso.so57943963foreignkeys D/GETCARDSBYNAME: Card Name is 7/
Value is 7
CardID is 23
In Folder NormalCards
In Category Hearts
2019-09-16 15:01:46.094 19140-19140/aso.so57943963foreignkeys D/GETCARDSBYNAME: Card Name is 7/
Value is 7
CardID is 36
In Folder NormalCards
In Category Clubs
2019-09-16 15:01:46.101 19140-19140/aso.so57943963foreignkeys D/GETCARDSBYNAME: Card Name is 7/
Value is 7
CardID is 49
In Folder NormalCards
In Category Diamonds
2019-09-16 15:01:46.107 19140-19140/aso.so57943963foreignkeys D/GETCARDSBYNAME: Card Name is 7/
Value is 7
CardID is 62
In Folder NormalCards
In Category ooops
2019-09-16 15:01:46.110 19140-19140/aso.so57943963foreignkeys D/GETCARDSBYFUNNYNAME: Card Name is 7/
Value is 7
CardID is 10
In Folder NormalCards
In Category Spades
2019-09-16 15:01:46.114 19140-19140/aso.so57943963foreignkeys D/GETCARDSBYFUNNYNAME: Card Name is 7/
Value is 7
CardID is 23
In Folder NormalCards
In Category Hearts
2019-09-16 15:01:46.118 19140-19140/aso.so57943963foreignkeys D/GETCARDSBYFUNNYNAME: Card Name is 7/
Value is 7
CardID is 36
In Folder NormalCards
In Category Clubs
2019-09-16 15:01:46.121 19140-19140/aso.so57943963foreignkeys D/GETCARDSBYFUNNYNAME: Card Name is 7/
Value is 7
CardID is 49
In Folder NormalCards
In Category Diamonds
2019-09-16 15:01:46.122 19140-19140/aso.so57943963foreignkeys D/GETCARDSBYFUNNYNAME: Card Name is 7/
Value is 7
CardID is 62
In Folder NormalCards
In Category ooops