I have an SQLite database with some tables containing several thousands of rows. I use GreenDAO to manage this database (but it could be another ORM).
What is the best way to add a lot of data in my database ?
1 - Using the conventional way of the ORM with thousands of lines like this one ? (GreenDAO in this example)
daoSession.getDefinitionDao().insert(new Definition(null,4L,"ail"));
This could be messy and get code too large errors on the way.
2 - Use an already populated database in the asset folder ?
Copy this already populated database from the asset folder to the data folder of the app using a method similar to this one :
/**
* Copy existing database file in system
*/
public void copyDataBase() {
int length;
byte[] buffer = new byte[1024];
String databasePath = DB_PATH + DB_NAME;
try {
InputStream databaseInputFile = this.context.getAssets().open(DB_NAME+".sqlite");
OutputStream databaseOutputFile = new FileOutputStream(databasePath);
while ((length = databaseInputFile.read(buffer)) > 0) {
databaseOutputFile.write(buffer, 0, length);
databaseOutputFile.flush();
}
databaseInputFile.close();
databaseOutputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
3 - Use raw queries got from a file containing a pure SQL script with all my inserts ?
SQLiteDatabase.execSQL(MY_RAW_SQL_INSERT_QUERY)
4 - Any other method ?