Executing multiple statements with SQLiteDatabase.execSQL

前端 未结 5 1514
暖寄归人
暖寄归人 2020-12-08 04:12

I\'ve followed a standard tutorial for building a database with Android. I\'ve created a class called DbHelper which extends SQLiteOpenHelper. I\'ve Overridden the create

5条回答
  •  北海茫月
    2020-12-08 04:57

    That's not possible to do using the standard methods which comes with Android. So, if you want to execute batch of multiple SQL statements, you will have to create your own utility to do so. For instance, you can have something like this:

    public void executeBatchSql(String sql){
        // use something like StringTokenizer to separate sql statements
        for each sql statement{
            database.execSQL(oneStatement);
        }
    }
    

    Though, what I'd do is something like this:

    String sql1 = "create bla bla bla;";
    String sql2 = "create foo bar;";
    String[] statements = new String[]{sql1, sql2};
    
    // then
    for(String sql : statements){
        database.execSQL(sql);
    }
    

提交回复
热议问题