1
android
runtime_error
ai_generated
true
android.database.sqlite.SQLiteException: 语法错误 near "?": (code 1): , 编译时: SELECT * FROM users WHERE id = ? AND name = ?
android.database.sqlite.SQLiteException: near "?": syntax error (code 1): , while compiling: SELECT * FROM users WHERE id = ? AND name = ?
ID: android/room-query-parameter-mismatch
95%修复率
90%置信度
1证据数
2023-02-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| Room 2.4.0 | active | — | — | — |
| Room 2.5.0 | active | — | — | — |
| Room 2.6.0 | active | — | — | — |
根因分析
Room @Query 中的参数数量与方法参数数量不匹配,导致 SQL 语法错误。
English
The number of parameters in a Room @Query does not match the number of method arguments, causing a SQL syntax error.
官方文档
https://developer.android.com/training/data-storage/room/accessing-data#query解决方案
-
确保 SQL 查询中的 '?' 占位符数量与方法参数数量匹配。例如:如果方法是 'fun getUser(id: Int, name: String): User?',查询应为 'SELECT * FROM users WHERE id = ? AND name = ?'。
-
使用命名绑定参数语法 ':paramName' 以提高清晰度并避免计数不匹配。例如:'SELECT * FROM users WHERE id = :id AND name = :name'。
-
运行 Room 的注解处理器(kapt 或 ksp)以在编译时生成验证错误,捕获参数不匹配问题。
无效尝试
常见但无效的做法:
-
80% 失败
The SQL query still has fewer placeholders than arguments, causing a mismatch error.
-
90% 失败
This introduces SQL injection risk and does not fix the parameter count issue.