Creating android app Database with big amount of data

后端 未结 4 418
轮回少年
轮回少年 2020-12-17 01:16

The database of my application need to be filled with a lot of data, so during onCreate(), it\'s not only some create table sql instructions, there is a lot of

4条回答
  •  無奈伤痛
    2020-12-17 01:58

    It looks like you are passing all your sql statements in one string. That's a problem because execSQL expects "a single statement that is not a query" (see documentation [here][1]). Following is a somewhat-ugly-but-working solution.

    I have all my sql statements in a file like this:

    INSERT INTO table1 VALUES (1, 2, 3);

    INSERT INTO table1 VALUES (4, 5, 6);

    INSERT INTO table1 VALUES (7, 8, 9);

    Notice the new lines in between text(semicolon followed by 2 new lines) Then, I do this:

    String text = new String(buffer, "UTF-8");
    for (String command : text.split(";\n\n")) { 
       try { command = command.trim(); 
       //Log.d(TAG, "command: " + command); 
       if (command.length() > 0) 
          db.execSQL(command.trim()); 
    }
    catch(Exception e) {do whatever you need here}
    

    My data columns contain blobs of text with new lines AND semicolons, so I had to find a different command-separator. Just be sure to get creative with the split str: use something you know doesn't exist in your data.

    HTH Gerardo

    [1]: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#execSQL(java.lang.String, java.lang.Object[])

提交回复
热议问题