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クラス:。私はコピーファイル方法を:(取ったところ
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();
}
}
}
}
}
私は本当に忘れ
1つの注意点があります:ユーザーがアプリのデータを消去すると、データベースはデフォルトに戻ります。
読解力0/10 ... https://github.com/jgilfelt/android-sqlite-asset-helper#upgrades-via-overwrite – Selvin
@Selvinあなたは正しいです、それあなたはその質問を読んでいないようですね! ;-) – PatriceG
"アセットファイルから新しいデータベースを読み込むために、アプリケーション内のデータベースからすべてのデータを削除してください。" ==強制的にロードする – Selvin