如何使用contentResolver的delete方法来保证安全注入?

12

您可以通过URI或向where参数传递一些参数来使用内容解析器进行删除。

如何使这些参数具有SQL注入安全性?
是否可以在ContentResolver中使用预处理语句?

act.getContentResolver().delete(myuriwithid,null,null);

act.getContentResolver().delete(mybaseuri," name = '"+this.name"'",null);
1个回答

19

使用位置参数。

public final int delete (Uri url, String where, String[] selectionArgs)

例如

ContentResolver cr = ...;
String where = "nameid=?";
String[] args = new String[] { "george" };
cr.delete( Stuff.CONTENT_URI, where, args );

我不知道。如果你不信任它,你可以使用 SQLiteDatabase.execSQL("delete..") 并自己加强查询。 - Gavin Bong
根据我的测试,它似乎是SQL注入安全的,但我没有在文档中看到相关记录。 - cprcrack
我刚刚发现了一些有用的信息,请在此页面中查找“SQL注入”:https://developer.android.com/training/contacts-provider/retrieve-names.html - cprcrack

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接