简体   繁体   English

如何防止android中的sql注入攻击?

[英]how to prevent sql injection attack in android?

so i have problems with following code and was wondering if there is any other way of creating and deleting table where table name is taken from user the following code gives error when using " ' " single quote. 所以我在以下代码上遇到了问题,并且想知道是否还有其他创建和删除表的方式,其中从用户那里获取表名称时,以下代码在使用“”单引号时给出了错误。 please help 请帮忙

public void droptable(String tablename) {
    SQLiteDatabase db =this.getWritableDatabase();
    db.execSQL("DROP TABLE IF EXISTS " + tablename);

}
 public void createtable(String tablename)
{
    SQLiteDatabase db = this.getWritableDatabase();
    String query = String.format("CREATE TABLE IF NOT EXISTS %s ( %s INTEGER PRIMARY KEY AUTOINCREMENT , %s TEXT )",tablename,COLUMN0,COLUMN1);
    db.execSQL(query);
}

is there a better way to do this? 有一个更好的方法吗?

Unless your app will export the SQLite database itself, with the user intending to use that SQLite database elsewhere, the user will never see this table except through your app. 除非您的应用程序将导出SQLite数据库本身,并且用户打算在其他地方使用该SQLite数据库,否则除非通过您的应用程序,否则该用户将永远不会看到此表。

That gives you a number of options: 这给您许多选择:

  1. Do not use the user-entered value for the table name. 请勿将用户输入的值用作表名。 Instead, generate a valid table name yourself, and keep a mapping of the generated names and what the user-entered "display name" is for each table. 而是自己生成一个有效的表名,并保留生成的名称与每个表的用户输入“显示名称”的映射。

  2. Transform the user-entered value into a valid table name, by removing unsupported characters. 通过删除不支持的字符,将用户输入的值转换为有效的表名。

  3. Wrap the user-entered value in quotation marks ( "%s" ), though then you will have problems if the user enters a table name with a quotation mark. 将用户输入的值括在引号( "%s" )中,但是如果用户输入带引号的表名,则会遇到问题。

  4. Prevent the user from entering an invalid table name, by rejecting non-standard characters as part of your EditText 通过拒绝非标准字符作为您的EditText一部分,防止用户输入无效的表名

  5. Validate the user-entered value and show an error dialog if they type in something that will result in SQL errors 验证用户输入的值并显示错误对话框(如果键入)会导致SQL错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM