在某些Android 9、10设备上使用CASE时出现SQLiteException,如果为真

h43kikqp  于 2023-02-27  发布在  Android
关注(0)|答案(1)|浏览(93)

以下SQL语句在我的Android测试设备(Android 13)上运行良好:

SELECT *
FROM portfolio
JOIN holdingCalculations
ON portfolio.uid = holdingCalculations.uid
ORDER BY calc_num_transactions = 0, CASE use_local_currency_for_totals WHEN TRUE THEN calc_total_change_percent_local ELSE calc_total_change_percent END DESC, portfolio.name COLLATE NOCASE

然而,在我发布Android应用进行生产后,我在一些客户设备上看到了异常(例如运行Android 10的POCO F1,运行Android 9的Galaxy S8+):

Fatal Exception: android.database.sqlite.SQLiteException: no such column: TRUE (code 1 SQLITE_ERROR[1]): , while compiling: SELECT *
FROM portfolio
JOIN holdingCalculations
ON portfolio.uid = holdingCalculations.uid
ORDER BY calc_num_transactions = 0, CASE use_local_currency_for_totals WHEN TRUE THEN calc_total_change_percent_local ELSE calc_total_change_percent END DESC, portfolio.name COLLATE NOCASE
       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(SQLiteConnection.java)
       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1229)
       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:703)
       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
       at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
       at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
       at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1865)
       at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1840)
       at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.query(FrameworkSQLiteDatabase.kt:156)
       at com.squareup.sqldelight.android.AndroidQuery.executeQuery(AndroidSqliteDriver.kt:253)
       at com.squareup.sqldelight.android.AndroidQuery.executeQuery(AndroidSqliteDriver.kt:228)
       at com.squareup.sqldelight.android.AndroidSqliteDriver$executeQuery$2.invoke(AndroidSqliteDriver.kt:149)
       at com.squareup.sqldelight.android.AndroidSqliteDriver$executeQuery$2.invoke(AndroidSqliteDriver.kt:149)
       at com.squareup.sqldelight.android.AndroidSqliteDriver.execute(AndroidSqliteDriver.kt:127)
       at com.squareup.sqldelight.android.AndroidSqliteDriver.executeQuery(AndroidSqliteDriver.kt:149)
       at com.squareup.sqldelight.db.SqlDriver$DefaultImpls.executeQuery$default(SqlDriver.kt:35)

我猜这种sqlite语法在某些设备上不受支持。这可能是安装在这些设备上的sqlite版本的问题,是否有一种向后兼容的方式来编写此查询?

fkvaft9z

fkvaft9z1#

使用true/false需要SQLite 3.23.0或更高版本,只有在API 30(Android 11)之后,SQLite版本才足够高(3.28.0)。
除非您将分布限制为11+,否则应该使用0表示false,1表示true。
你可参阅:
https://www.sqlite.org/lang_expr.html#boolean_expressions
以及
Version of SQLite used in Android?

相关问题