2016-07-31 5 views
0

Ich mag würde eine temporäre Datenbank erstellen, dann ist dies der Code:doppelte Standarddatenbank in temporärer Datenbank

String PATH = "/data/data/" + appContext.getPackageName() + "/databases/"; 
      List<File> files = getListFiles(new File(PATH)); 
      //File dbFile = appContext.getDatabasePath(PreferenceConstants.TEMP_DB_DATABASE_STORE); 
      File dbFile = new File(PATH, PreferenceConstants.TEMP_DB_DATABASE_STORE); 
      FileInputStream is; 

      if (!dbFile.exists()) { 
       try { 
        dbFile.createNewFile(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      }else 
       dbFile.delete(); 
      try { 
       is = new FileInputStream(appContext.getDatabasePath(PreferenceConstants.DB_DATABASE_STORE)); 
       FileUtils.copyInputStreamToFile(is, dbFile); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

aber keine Datei in Datenbank-Ordner erstellt, warum?

nach der Kopie möchte ich eine andere Datenbank öffnen, kopieren Sie die Tabellen und fügen Sie die Tabelle in der Temp-Datenbank erstellt.

EDIT

sehr einfache Methode:

DatabaseHelper dbIng = new DatabaseHelper(appContextDialog, "temp_database.db"); 
dbIng.closeDB(); 

Antwort

1

Dies ist, was benutze ich für den Import und Export von Datenbanken: Dont über die Berechtigungen vergessen.

public void exportDatabase(){ 
    try 
    { 
     File sd = Environment.getExternalStorageDirectory(); 
     File data = Environment.getDataDirectory(); 

     String currentDBPath = "//data//MY.PACKAGE.NAME//databases//MY_DATABASE_NAME"; 
     String backupDBPath = "MY_DATABASE_FILE.db"; 
     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(c, c.getResources().getString(R.string.exporterenToast), Toast.LENGTH_SHORT).show(); 
    } 
    catch (Exception e) { 
     Toast.makeText(c, c.getResources().getString(R.string.portError), Toast.LENGTH_SHORT).show(); 
     Log.d("Main", e.toString()); 
    } 
} 

public void importDatabase(){ 
    try 
    { 
     File sd = Environment.getExternalStorageDirectory(); 
     File data = Environment.getDataDirectory(); 

     String currentDBPath = "//data//" + "MY.PACKAGE.NAME" + "//databases//" + "MY_DATABASE_NAME"; 
     String backupDBPath = "MY_DATABASE_FILE.db"; 
     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(c, c.getResources().getString(R.string.importerenToast), Toast.LENGTH_LONG).show(); 
    } 
    catch (Exception e) { 
     Toast.makeText(c, c.getResources().getString(R.string.portError), Toast.LENGTH_SHORT).show(); 
    } 
} 
Verwandte Themen