i am developing an application in which i want to read an sqlite database file from the asset folder of the project. i have searched the web but nothing found helpful. please help me.
Thanks.
-
5Did you really find nothing? Web is a waste man, lets smoke come'on!Junaid– Junaid2013-12-31 12:56:58 +00:00Commented Dec 31, 2013 at 12:56
-
google.com.au/…Junaid– Junaid2013-12-31 12:57:51 +00:00Commented Dec 31, 2013 at 12:57
-
1Better to go through this link stackoverflow.com/questions/2605555/…Elshan– Elshan2014-06-11 21:36:02 +00:00Commented Jun 11, 2014 at 21:36
Add a comment
|
2 Answers
Basically you can get an InputStream (from the AssetManager open function) for a file name and write it to an OutputStream.
InputStream inputStream = getAssets().open(fileName);
If you create your database using openOrCreateDatabase you might want to put the database in the databases folder...
String fileName = "MySQLiteDB.db";
File file = getDatabasePath(fileName );
if (!file.exists()) {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdir();
}
InputStream inputStream = getAssets().open(DATABASE_NAME);
OutputStream outputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 8];
int numOfBytesToRead;
while((numOfBytesToRead = inputStream.read(buffer)) > 0)
outputStream.write(buffer, 0, numOfBytesToRead);
inputStream.close();
outputStream.close();
}
db = SQLiteDatabase.openOrCreateDatabase(file, null);