J’essaie d’implémenter une simple exportation / importation SQLite
à des fins de sauvegarde. L’exportation consiste simplement à stocker une copie du fichier raw current.db
. Ce que je veux faire pour importer, c’est simplement supprimer l’ancien fichier current.db
et renommer le current.db
en current.db
. Est-ce possible? Lorsque j’essaie cette solution, j’obtiens l’erreur suivante:
06-30 13:33:38.831: ERROR/SQLiteOpenHelper(23570): android.database.sqlite.SQLiteDatabaseCorruptException: error code 11: database disk image is malformed
Si je regarde le fichier de firebase database brut dans un navigateur SQLite
cela semble correct.
J’utilise ce code dans SQLiteOpenHelper
dans l’une de mes applications pour importer un fichier de firebase database.
EDIT: J’ai collé ma méthode FileUtils.copyFile()
dans la question.
SQLiteOpenHelper
public static Ssortingng DB_FILEPATH = "/data/data/{package_name}/databases/database.db"; /** * Copies the database file at the specified location over the current * internal application database. * */ public boolean importDatabase(Ssortingng dbPath) throws IOException { // Close the SQLiteOpenHelper so it will commit the created empty // database to internal storage. close(); File newDb = new File(dbPath); File oldDb = new File(DB_FILEPATH); if (newDb.exists()) { FileUtils.copyFile(new FileInputStream(newDb), new FileOutputStream(oldDb)); // Access the copied database so SQLiteHelper will cache it and mark // it as created. getWritableDatabase().close(); return true; } return false; }
FileUtils
public class FileUtils { /** * Creates the specified
toFile
as a byte for byte copy of the *fromFile
. IftoFile
already exists, then it * will be replaced with a copy offromFile
. The name and path * oftoFile
will be that oftoFile
.
*
* Note:fromFile
andtoFile
will be closed by * this function. * * @param fromFile * - FileInputStream for the file to copy from. * @param toFile * - FileInputStream for the file to copy to. */ public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException { FileChannel fromChannel = null; FileChannel toChannel = null; try { fromChannel = fromFile.getChannel(); toChannel = toFile.getChannel(); fromChannel.transferTo(0, fromChannel.size(), toChannel); } finally { try { if (fromChannel != null) { fromChannel.close(); } } finally { if (toChannel != null) { toChannel.close(); } } } } }
N’oubliez pas de supprimer l’ancien fichier de firebase database si nécessaire.
Ceci est une méthode simple pour exporter la firebase database vers un dossier nommé dossier de sauvegarde, vous pouvez le nommer comme vous le souhaitez et une méthode simple pour importer la firebase database à partir du même dossier a
public class ExportImportDB extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //creating a new folder for the database to be backuped to File direct = new File(Environment.getExternalStorageDirectory() + "/Exam Creator"); if(!direct.exists()) { if(direct.mkdir()) { //directory is created; } } exportDB(); importDB(); } //importing database private void importDB() { // TODO Auto-generated method stub try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { Ssortingng currentDBPath= "//data//" + "PackageName" + "//databases//" + "DatabaseName"; Ssortingng backupDBPath = "/BackupFolder/DatabaseName"; File backupDB= new File(data, currentDBPath); File currentDB = new File(sd, backupDBPath); FileChannel src = new FileInputStream(currentDB).getChannel(); FileChannel dst = new FileOutputStream(backupDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); Toast.makeText(getBaseContext(), backupDB.toSsortingng(), Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(getBaseContext(), e.toSsortingng(), Toast.LENGTH_LONG) .show(); } } //exporting database private void exportDB() { // TODO Auto-generated method stub try { File sd = Environment.getExternalStorageDirectory(); File data = Environment.getDataDirectory(); if (sd.canWrite()) { Ssortingng currentDBPath= "//data//" + "PackageName" + "//databases//" + "DatabaseName"; Ssortingng backupDBPath = "/BackupFolder/DatabaseName"; File currentDB = new File(data, currentDBPath); File backupDB = new File(sd, backupDBPath); FileChannel src = new FileInputStream(currentDB).getChannel(); FileChannel dst = new FileOutputStream(backupDB).getChannel(); dst.transferFrom(src, 0, src.size()); src.close(); dst.close(); Toast.makeText(getBaseContext(), backupDB.toSsortingng(), Toast.LENGTH_LONG).show(); } } catch (Exception e) { Toast.makeText(getBaseContext(), e.toSsortingng(), Toast.LENGTH_LONG) .show(); } } }
N’oubliez pas d’append cette autorisation pour procéder
Prendre plaisir