암호화된 위치로 데이터베이스 복사
BlackBerry 스마트폰의 암호화된 저장 위치(eMMC 또는 microSD 카드)로 SQLite 데이터베이스를 복사하면 데이터베이스가 Database API에 액세스하지 못하게 될 수도 있습니다. 이는 미디어 카드가 Database API에서 사용하는 암호화와는 다른 유형의 암호화를 사용하기 때문입니다.
데이터베이스를 복사하여 액세스하려면 Database API를 사용하여 빈 데이터베이스 파일을 만들고, 파일을 0으로 만든 다음 데이터베이스를 해당 파일로 복사해야 합니다.
다음 코드 샘플은 이와 같은 기술을 보여 줍니다.
// Copy a database to an encrypted storage location.
private void copyDBRecommendedWay() {
try {
String dbPath = "file:///SDCard/original.db";
String dbPathCopy = "file:///SDCard/copy.db";
Database dbCopy = null;
try {
// Delete if there's an existing one.
DatabaseFactory.delete(URI.create(dbPathCopy));
// Create the database using the DatabaseFactory class.
dbCopy = DatabaseFactory.create(URI.create(dbPathCopy));
} catch (DatabaseException e) {
System.out.println( "DatabaseException: error code: "
+ e.getErrorCode() + " msg: " + e.getMessage());
} finally {
// Close the database.
dbCopy.close();
}
// Open a connection to the database file that was created.
FileConnection outfc = (FileConnection)Connector.open(dbPathCopy);
// Truncate the file.
outfc.truncate(0);
// Write out the downloaded database data to FileConnection.openOutputStream().
OutputStream os = outfc.openOutputStream();
FileConnection infc = (FileConnection)Connector.open(dbPath);
InputStream is = infc.openInputStream();
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0){
os.write(buf, 0, len);
}
is.close();
os.close();
// Close the file connection.
outfc.close();
System.out.println("Copied " + dbPath + " to " + dbPathCopy);
// The database can now be reopened with the DatabaseFactory class.
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
다음 주제: 데이터베이스 열기 및 닫기
이전 주제: 코드 샘플: SQLite 데이터베이스 삭제