2012-04-13 4 views
0

SQLite db 파일을 업데이트하는 응용 프로그램이 있지만이 응용 프로그램을 제거 할 때마다이 db 파일이 삭제되고 다시 설치하면 새 db 파일이 만들어집니다. 나는이 데이터베이스 파일을 마이크로 SD 카드로 업데이트 할 때마다 복사하여 설치 제거 후에 데이터베이스에 액세스 할 수있게하려고합니다.백업용으로 SQLite db를 저장 장치에 복사

목표 복사 DB 파일이

  • 을 만들 때

    1. 가 지금은 마이크로 SD 카드에
    2. 이 DB 파일을이 DB 파일을 복사 할 text.db 말 업데이트 될 때마다 (응용 프로그램 DB 파일) 지금
    3. 업데이트 복사 마이크로 SD 카드에 번째 복사
  • 답변

    1

    도우 코드를이 DB 파일을 대체되고있다 전자 데이터베이스를 SD 카드에 저장합니다. 다시 복사하려면 스트림을 뒤집기 만하면됩니다.

      public boolean copyDatabase() { 
          String SDCardPath = Environment.getExternalStorageDirectory().getAbsolutePath(); 
          // Create the directory if neccesary. 
          File directory = new File(SDCardPath + <PATH TO SD-CARD SAVE LOCATION>); 
          if (!directory.exists()) 
            directory.mkdir(); 
    
    
          // Close the database before trying to copy it 
          database.close(); 
    
          // Copy database to SD-card 
          try { 
            InputStream mInput = new FileInputStream(<PATH TO DATABASE ON PHONE>); 
            OutputStream mOutput = new FileOutputStream(SDCardPath + <PATH TO SD-CARD SAVE LOCATION>); 
    
            byte[] buffer = new byte[1024]; 
            int length; 
            while ((length = mInput.read(buffer)) > 0) { 
              mOutput.write(buffer, 0, length); 
            } 
            mOutput.flush(); 
            mOutput.close(); 
            mInput.close(); 
          } catch (Exception e) { 
    
          } 
    
          return database.open(); 
        } 
    
    관련 문제