Is it possible to supply prefilled SQLite DB to my app? I want to use db as ordinary SQLite db which will have tables filled manually and I want to include it into my .apk file.
Asked
Active
Viewed 3,379 times
0
-
Will the SQLite database be used read-only or will you app add data to it? – Robert Feb 19 '11 at 16:37
-
refer the link https://stackoverflow.com/questions/513084/ship-an-application-with-a-database – Shyju M Sep 27 '17 at 11:42
-
Refer the link https://stackoverflow.com/questions/513084/ship-an-application-with-a-database – Shyju M Sep 27 '17 at 11:45
3 Answers
3
Yes, include it in your assets folder and copy it into the /databases folder when your application first launches.

smith324
- 13,020
- 9
- 37
- 58
-
-
See my comment here. http://stackoverflow.com/questions/4753896/externally-generated-sqlite-database-considered-read-only-by-androids-sqliteopen/4753927#4753927 – Nick Campion Feb 19 '11 at 17:35
3
try this
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}

Phantômaxx
- 37,901
- 21
- 84
- 115

Rohit Mandiwal
- 10,258
- 5
- 70
- 83
-
I keep receiving FileNotFound exception when app attempts to open OutputStream. In fact, on first start there is no /database/ dir and app fails to create it. – Feb 21 '11 at 18:35
-
Fixed that, it's necessary to create both dir and file explicitly, thnx to all! – Feb 21 '11 at 18:57
-
The previous code is the correct solution. I've done this on one of my application, you can find more information here : http://androidblogger.blogspot.com/2009/05/how-to-ship-application-with-pre-baked.html But finally I decided to stop using this technique, as it implies the database is present twice in your application ( once in the asset folder, once in your application folder ), and for a big database that can be problematic. – alocaly Feb 20 '11 at 02:53
-
How should I treat journal file? Should I add to assets and copy it as well? – János Sep 21 '16 at 13:51
0
Including in the assets folder is one option (Apk size will be increased.)
How about storing the pre-filled db on any cloud storage service provider and downloading the file on the first run of the app?

Shree Harsha S
- 665
- 7
- 14