Creating android app Database with big amount of data

后端 未结 4 419
轮回少年
轮回少年 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[])

    0 讨论(0)
  • 2020-12-17 02:05

    I think your problem is in this line:

    return new String(buffer);
    

    You're converting the array of bytes in to a java.lang.String but you're not telling Java/Android the encoding to use. So the bytes for your accented characters aren't being converted correctly as the wrong encoding is being used.

    If you use the String(byte[],<encoding>) constructor you can specify the encoding your file has and your characters will be converted correctly.

    0 讨论(0)
  • 2020-12-17 02:06

    I am using a different approach: Instead of executing loads of sql statements (which will take long time to complete), I build my sqlite database on the desktop, put it in the assets folder, create an empty sqlite db in android and copy the db from the assets folder into the database folder. This is a huge increase in speed. Note, you need to create an empty database first in android, and then you can copy and overwrite it. Otherwise, Android will not allow you to write a db into the datbase folder. There are several examples on the internet. BTW, seems this approach works best, if the db has no file extension.

    0 讨论(0)
  • 2020-12-17 02:10

    The SQL file solution seems perfect, it's just that you need to make sure that the file is saved in utf8 encoding otherwise all the accentuated characters will be lost. If you don't want to change the file's encoding then you need to pass an extra argument to new String(bytes, charset) defining the file's encoding.

    Do prefer to use file resources instead of static final String to avoid having all those unnecessary bytes loaded into memory. In mobile phones you want to save all memory possible!

    0 讨论(0)
提交回复
热议问题