I am creating an Android Application and I am using sqlite database in it. for that I have placed a sqlite file in assets folder of project and I am copying this file to phone while my first execution of application using code below.
private void copyDataBase() throws IOException {
new File(DB_PATH).mkdirs();
InputStream myInput = appContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
but I am getting this errors.
09-21 18:03:56.841: E/SQLiteLog(7850): (1) no such table: tbl_player
but this table is exists in assets file. so I fetched database file from phone using this method.
public static void exportDB(String databaseName, Context context) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + context.getPackageName()
+ "//databases//" + databaseName + "";
String backupDBPath = "sensor_game.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB)
.getChannel();
FileChannel dst = new FileOutputStream(backupDB)
.getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
} catch (Exception e) {
}
}
and I found that there is no table in fetched database file.
Note: This issue is occurs only in OnePlus Two
and working fine in Nexus 4
, Htc 820
,Moto E
,Galxy S3
and Galaxy Quottro
void exportDB
. Make that boolean and add code in case currentDB does not exist. That code will/can not work. – Landgravestatement aborts at 15: [SELECT locale FROM android_metadata UNION SELECT NULL ORDER BY locale DESC LIMIT 1] 10-13 14:20:35.633 E/SQLiteDatabase(15040): Failed to open database '/data/data/myapp/databases/app.db'. 10-13 14:20:35.633 E/SQLiteDatabase(15040): android.database.sqlite.SQLiteException: Failed to change locale for db '/data/data/myapp/databases/app.db' to 'de_DE'.
– LebruncopyDataBase
andexportDB
methods executing properly without any Exception ? – Denby