Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating android app Database with big amount of data
    primarykey
    data
    text
    <p>The database of my application need to be filled with a lot of data, so during <code>onCreate()</code>, it's not only some create table sql instructions, there is a lot of inserts. The solution I chose is to store all this instructions in a sql file located in res/raw and which is loaded with <code>Resources.openRawResource(id)</code>.</p> <p>It works well but I face to encoding issue, I have some accentuated caharacters in the sql file which appears bad in my application. This my code to do this:</p> <pre><code>public String getFileContent(Resources resources, int rawId) throws IOException { InputStream is = resources.openRawResource(rawId); int size = is.available(); // Read the entire asset into a local byte buffer. byte[] buffer = new byte[size]; is.read(buffer); is.close(); // Convert the buffer into a string. return new String(buffer); } public void onCreate(SQLiteDatabase db) { try { // get file content String sqlCode = getFileContent(mCtx.getResources(), R.raw.db_create); // execute code for (String sqlStatements : sqlCode.split(";")) { db.execSQL(sqlStatements); } Log.v("Creating database done."); } catch (IOException e) { // Should never happen! Log.e("Error reading sql file " + e.getMessage(), e); throw new RuntimeException(e); } catch (SQLException e) { Log.e("Error executing sql code " + e.getMessage(), e); throw new RuntimeException(e); } </code></pre> <p>The solution I found to avoid this is to load the sql instructions from a huge <code>static final String</code> instead of a file, and all accentuated characters appear well.</p> <p>But isn't there a more elegant way to load sql instructions than a big <code>static final String</code> attribute with all sql instructions?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload