我试图阻止在我的触发器中注入(inject)(创建或不创建取决于偏好)。
我正在使用
public void execSQL (String sql, Object[] bindArgs)
我的触发器有一个“?”并在 bindArgs 中传递我的值。
问题是我在创建触发器的过程中收到错误
03-13 13:43:49.136: E/SQLiteLog(21865): (1) trigger cannot use variables
03-13 13:43:49.137: W/System.err(21865): android.database.sqlite.SQLiteException: trigger cannot use variables (code 1): , while compiling:
CREATE TRIGGER IF NOT EXISTS
my_trigger
INSERT ON table_1 BEGIN DELETE
FROM table_2
WHERE col1 = new.col1
AND col2 = ?
END;
看起来我不能在创建触发器查询时使用参数,但我不明白为什么。如果我替换“?”我自己在字符串中有一个值并使用
public void execSQL (String sql)
这项工作很好,但我想创建一个带有字符串值的触发器,我不想检查 SQLInjection。
任何人都可以解释为什么我不能使用
public void execSQL (String sql, Object[] bindArgs)
对于触发器(即使 android documentation 另有说明)
For example, the following are good candidates for using this method:
CREATE or DROP table / trigger / view / index / virtual table
...
最佳答案
错误信息非常明确:
trigger cannot use variables
即你不能在触发器中使用 ?
变量。
仅使用不带变量的字符串格式。由于该参数是一个整数,因此无需为注入(inject)清理它,假设您将其作为整数传递。
对于字符串,有 DatabaseUtils.sqlEscapeStrings()
.
"the following are good candidates for using this method: CREATE or DROP trigger"
这实际上意味着 execSQL()
重载之一应该用于像这样的 SQL,并且在 Android SQLite 包装器中没有更好的选择。这并不意味着变量绑定(bind)特别适合与此类 SQL 一起使用。
Android sqlite 的文档不是很好。
Is it not Android who "replace" the '?'s to send a correct query to SQLite ?
不,the ?
parameters绑定(bind)在 sqlite 中。
关于android - 使用带参数的 SQLite 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29032750/