SQLiteDatabase.query是否使用PreparedStatements?

问题描述 投票:1回答:1

当我使用Android中的用户输入查询我的数据库时,我通常使用SQliteDatabase.query,正确使用SelectionArgs。

Cursor cursor = db.query(UserEntity.TABLE,
            new String[]{UserEntity.COLUMN_ID, UserEntity.COLUMN_USERNAME, UserEntity.COLUMN_FIRSTNAME,UserEntity.COLUMN_LASTNAME,...},
            UserEntity.COLUMN_USERNAME + "=?",
            new String[]{username},
            null, null, null);

是否足以阻止SQL注入?或者我之前应该使用PreparedStatements吗?

android sqlite prepared-statement
1个回答
1
投票

使用预准备语句的建议来自PHP,其中一些旧API不允许SQL参数(?),除非使用预准备语句。

但参数和预处理语句是正交概念。要防止注入(并避免字符串/ blob格式化问题),您只需要参数。 (而query()给你的。)

只有在想要多次执行时,预准备语句才有用。

(实际上,query()确实在内部使用了一个准备好的语句,但这是SQLite API的结果,对你来说并不关心。)

© www.soinside.com 2019 - 2024. All rights reserved.