mercredi 4 mars 2015

my sqlite database is read only

i have a class that copy database from asset to /data/data


it works when i use select query. but when i use insert query return error that my database is read only my class code is :



public class AssetDatabaseOpenHelper {

private static final String DB_NAME = "database";

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();
}


}


Aucun commentaire:

Enregistrer un commentaire