-
-
Save wontondon/1271795 to your computer and use it in GitHub Desktop.
package com.javatarts.basketballgm.data; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
public class AssetDatabaseOpenHelper { | |
private static final String DB_NAME = "asset.db"; | |
private Context context; | |
public AssetDatabaseOpenHelper(Context context) { | |
this.context = context; | |
} | |
public SQLiteDatabase openDatabase() { | |
File dbFile = context.getDatabasePath(DB_NAME); | |
if (!dbFile.exists()) { | |
try { | |
copyDatabase(dbFile); | |
} catch (IOException e) { | |
throw new RuntimeException("Error creating source database", e); | |
} | |
} | |
return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY); | |
} | |
private void copyDatabase(File dbFile) throws IOException { | |
InputStream is = context.getAssets().open(DB_NAME); | |
OutputStream os = new FileOutputStream(dbFile); | |
byte[] buffer = new byte[1024]; | |
while (is.read(buffer) > 0) { | |
os.write(buffer); | |
} | |
os.flush(); | |
os.close(); | |
is.close(); | |
} | |
} |
Dude could you help me PLEASE. How can i read from the file? or how can i be sure that the db was created succesful on my device?
Thank you very much!
Doesn't work :(
How to call this from activity? any example?
AssetDatabaseOpenHelper adb = new AssetDatabaseOpenHelper(this);
SQLiteDatabase db = adb.openDatabase();
Cursor c = db.rawQuery("SELECT * FROM xxx;", null);
Log.d("MyApp", "cnt: "+c.getCount());
Te Felicito!! De maravilla tu aportación!
Thanks for the handy code it copy file from assets folder to internal storage but can't read that db file ?
can you explain why ?
still no table found error i have checked using ddms, db file is there and table also exist but still the error.
#38 OutputStream os = new FileOutputStream(dbFile);
this line giving me the error "Caused by: java.io.FileNotFoundException: /data/data/com.example.test/databases/dbname: open failed: ENOENT (No such file or directory)"
can any one help me with this ?
Hi
This class don't work because it necessary create sqlite file before overwrite .
My solution is a modified version of openDatabase
public SQLiteDatabase openDatabase() {
File dbFile = context.getDatabasePath(DB_NAME);
if (!dbFile.exists()) {
try {
SQLiteDatabase checkDB = context.openOrCreateDatabase(DB_NAME, context.MODE_PRIVATE, null);
if(checkDB != null){
checkDB.close();
}
copyDatabase(dbFile);
} catch (IOException e) {
throw new RuntimeException("Error creating source database", e);
}
}
return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
}
Hi, I have a problem with my code:
When I was running my program on the simulator:
In logcat:
"No such file or directory (2)"
please help me .
: (10) Failed to do file read, got: 0, amt: 100, last Errno: 2
help me
Awesome! Thanks!
Thank you brother <3
Thanks! This was really useful.