Executing multiple statements with SQLiteDatabase.execSQL

前端 未结 5 1526
暖寄归人
暖寄归人 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:42

    Well, in my case, I am excuting queries from a file which I saved as an asset This is the solution I used+-

    String script = readAsset(CREATE_SCRIPT);//readAsset is a method i use to get the file contents
    try {
         String[] queries = script.split(";");
     for(String query : queries){
            db.execSQL(query);
         }
     } catch (Exception e) {
        .....
    

    EDIT

    In my case, the Queries were simple insert queries which I had full control over. However, the issue has been raised concerning queries with ";" inside them.

    @TWiStErRob suggests using

    script.split(";$");// $ meaning end of line. You will need to use RegexOption.MULTILINE for this to work
    

    or

    script.split(";\n");// but you will need to ensure that each query is on a different line
    

提交回复
热议问题