this code (android-sqlite-asset-helper)을 사용하여 자산 폴더의 파일 위치에서 데이터베이스를로드합니다. 이것은 잘 작동합니다.Android : 데이터베이스에서 데이터 제거
그러나 데이터베이스를 새로 고치거나 업그레이드하는 과정은 간단하지 않으며 앱의 데이터베이스에서 모든 데이터를 수동으로 제거하는 간단한 방법이 있는지 궁금합니다. 자산 파일에서 새 데이터베이스를로드합니다.
this code (android-sqlite-asset-helper)을 사용하여 자산 폴더의 파일 위치에서 데이터베이스를로드합니다. 이것은 잘 작동합니다.Android : 데이터베이스에서 데이터 제거
그러나 데이터베이스를 새로 고치거나 업그레이드하는 과정은 간단하지 않으며 앱의 데이터베이스에서 모든 데이터를 수동으로 제거하는 간단한 방법이 있는지 궁금합니다. 자산 파일에서 새 데이터베이스를로드합니다.
주요 활동 작성 :
getApplicationContext().deleteDatabase("mydatabase.db");
기본 복사본 파일을 사용하여 기본 데이터베이스의 데이터를 재정의 할 수 있습니다. 기본 데이터베이스를 새 데이터베이스 파일로 덮어 쓰면됩니다. 다음 코드는 파일에서만 작동하므로 여기저기서 자산 파일과 함께 작동하도록 약간 변경해야합니다. 여기 데이터베이스 파일을 덮어 쓸 수있는 방법은
/**
* Copies the database file at the specified location over the current
* internal application database.
**/
public boolean importDatabase(Context context, String dbPath) throws IOException {
File OldDbFile = context.getApplicationContext().getDatabasePath(DBSchema.DATABASE_NAME);
// Close the SQLiteOpenHelper so it will commit the created empty
// database to internal storage.
close();
File newDb = new File(dbPath);
if (newDb.exists()) {
FileUtils.copyFile(new FileInputStream(newDb), new FileOutputStream(OldDbFile));
// Access the copied database so SQLiteHelper will cache it and mark
// it as created.
getWritableDatabase().close();
return true;
}
return false;
}
Fileutils의 클래스 :. 나는 CopyFile 수 방법 :(했다 어디
public class FileUtils {
/**
* Creates the specified <code>toFile</code> as a byte for byte copy of the
* <code>fromFile</code>. If <code>toFile</code> already exists, then it
* will be replaced with a copy of <code>fromFile</code>. The name and path
* of <code>toFile</code> will be that of <code>toFile</code>.<br/>
* <br/>
* <i> Note: <code>fromFile</code> and <code>toFile</code> will be closed by
* this function.</i>
*
* @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();
}
}
}
}
}
정말 잊지
하나주의해야 할 점은있다 : 사용자가 앱 데이터를 정리하면 데이터베이스가 기본값으로 돌아갑니다.
읽기 능력 0/10 ... https://github.com/jgilfelt/android-sqlite-asset-helper#upgrades-via-overwrite – Selvin
당신은 맞다 @Selvin, 그것을 당신이 그 질문을 읽지 않았던 것 같습니다! ;-) – PatriceG
"앱의 데이터베이스에서 모든 데이터를 제거하여 자산 파일에서 새 데이터베이스를로드하십시오." == 강제로드 – Selvin