Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SQLite for Android应用程序中准备好的语句编写INSERT、UPDATE、DELETE、SELECT和嵌套SELECT命令?_Android_Sqlite_Select_Prepared Statement_Nested Query - Fatal编程技术网

如何使用SQLite for Android应用程序中准备好的语句编写INSERT、UPDATE、DELETE、SELECT和嵌套SELECT命令?

如何使用SQLite for Android应用程序中准备好的语句编写INSERT、UPDATE、DELETE、SELECT和嵌套SELECT命令?,android,sqlite,select,prepared-statement,nested-query,Android,Sqlite,Select,Prepared Statement,Nested Query,最近我了解到android中的原始查询无法阻止SQL注入,因此我决定将所有查询转换为Prepared语句,这就是SQL注入预防。但我不知道如何转换准备语句中的复杂查询 我想转换以下查询: 一, 二, 防止SQL注入的唯一方法是使用参数。(在一些PHP API中,获取参数的唯一方法是使用准备好的语句,但这不是Android数据库API的缺点之一。) 只需为任何字符串写入?,并分别传递值: 字符串名=。。。; 字符串密码=。。。; cursor=db.rawQuery(“从Name=?和Passwo

最近我了解到android中的原始查询无法阻止SQL注入,因此我决定将所有查询转换为Prepared语句,这就是SQL注入预防。但我不知道如何转换准备语句中的复杂查询

我想转换以下查询:

一,

二,


防止SQL注入的唯一方法是使用参数。(在一些PHP API中,获取参数的唯一方法是使用准备好的语句,但这不是Android数据库API的缺点之一。)

只需为任何字符串写入
,并分别传递值:

字符串名=。。。;
字符串密码=。。。;
cursor=db.rawQuery(“从Name=?和Password=?”的用户中选择SomeCol”,
新字符串[]{name,password});

请注意,只有当您的字符串值由(可能有敌意的)用户控制时,才可能发生SQL注入。您上面的查询看起来不像是这种情况。

您可以使用
rawQuery
通过selectionargs(第二个参数)传递任何参数来防止注入

SQL注入不适用于这两个查询,因为它们是硬编码的,没有用户生成/提供的输入

e、 g.您的第一个查询可能是(为了演示,假设'Y'和CV.TRUE作为参数传递(即用户生成/提供):-

但是,通常建议使用方便方法,而不是
rawQuery
execSQL
,如果可以使用方便方法,也可以通过参数使用绑定字符串,使用
query
方便方法的上述方法可以是:-

public Cursor query1(String indicator1, String indicator2) {
    String whereclause = "(tab1col1 IN(SELECT tab2col2 FROM MasterTable WHERE tab2col1=?) OR tab1col2=?)";
    String[] whereargs = new String[] {indicator1,indicator2};
    String order_columns = "tab1col3,tab1col4,tab1col5,tab1col6";
    return mDB.query("TableName",null,whereclause,whereargs,null,null,order_columns);
}
您不会使用准备好的语句本身,因为它们仅限于返回单个值,而不是一行或多行


警告:恕不另行通知 但是,如果您真的愿意,您可以使用:-

public Cursor query1ps(String indicator1,String indicator2) {
    String[] whereargs = new String[] {indicator1,indicator2};
    SQLiteStatement stmnt = mDB.compileStatement("SELECT * " +
            " FROM TableName " +
            " WHERE (tab1col1" +
            "     IN(" +
            "         SELECT tab2col2 " +
            "         FROM MasterTable " +
            "         WHERE tab2col1=?)" +
            "     OR  tab1col2=?)" +
            " ORDER BY tab1col3, tab1col4,tab1col5,tab1col6");
    stmnt.bindAllArgsAsStrings(whereargs);
    Log.d("PREPAREDSQL",stmnt.toString());
    String sql = stmnt.toString().replace("SQLiteProgram:","");
    return mDB.rawQuery(sql,null);
}
  • 正如您所看到的,所有准备好的语句都是这样做的,它们正在替换参数,因此与其他方法相比没有什么好处。这还取决于SQLIteProgram:保持不变

如果您能为我提供解决方案而不是有疑问的建议@Kling Klangtanks@MikeT,那就太好了
public Cursor query1raw(String indicator1,String indicator2) {
    String sql = "SELECT * " +

            " FROM TableName " +
            " WHERE (tab1col1" +
            "     IN(" +
            "         SELECT tab2col2 " +
            "         FROM MasterTable " +
            "         WHERE tab2col1=?)" +
            "     OR  tab1col2=?)" +
            " ORDER BY tab1col3, tab1col4,tab1col5,tab1col6";
    String[] args = new String[]{indicator1,indicator2};
    return mDB.rawQuery(sql,args);
}
public Cursor query1(String indicator1, String indicator2) {
    String whereclause = "(tab1col1 IN(SELECT tab2col2 FROM MasterTable WHERE tab2col1=?) OR tab1col2=?)";
    String[] whereargs = new String[] {indicator1,indicator2};
    String order_columns = "tab1col3,tab1col4,tab1col5,tab1col6";
    return mDB.query("TableName",null,whereclause,whereargs,null,null,order_columns);
}
public Cursor query1ps(String indicator1,String indicator2) {
    String[] whereargs = new String[] {indicator1,indicator2};
    SQLiteStatement stmnt = mDB.compileStatement("SELECT * " +
            " FROM TableName " +
            " WHERE (tab1col1" +
            "     IN(" +
            "         SELECT tab2col2 " +
            "         FROM MasterTable " +
            "         WHERE tab2col1=?)" +
            "     OR  tab1col2=?)" +
            " ORDER BY tab1col3, tab1col4,tab1col5,tab1col6");
    stmnt.bindAllArgsAsStrings(whereargs);
    Log.d("PREPAREDSQL",stmnt.toString());
    String sql = stmnt.toString().replace("SQLiteProgram:","");
    return mDB.rawQuery(sql,null);
}