0

Android パッケージに大量のデータをパックしたいと考えています。これにデータベースを使用できますか? はいの場合、データベーステーブルを APK でパックする方法は?

4

3 に答える 3

1

データベースをassets/フォルダーに配置し、アプリケーションを初めて実行するときに、次のコードを使用してデータベースをあるべき場所にコピーします。

private void copyFromAssets() {
    InputStream istream = null;
    OutputStream ostream = null;
    try {
        istream = context.getAssets().open(DATABASE_NAME);
        File path = context.getDatabasePath(DATABASE_NAME);
        if (path.exists() == false)
            path.createNewFile();

        ostream = new FileOutputStream(path);

        byte[] buffer = new byte[8192];
        int length;
        while ((length = istream.read(buffer))>0) {
            ostream.write(buffer, 0, length);
        }
        ostream.flush();
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(TAG, "Failed to copy database: " + DATABASE_NAME);
    } finally {
        try {
            if (ostream != null) ostream.close();
            if (istream != null) istream.close();
        } catch (IOException e) {}
    }
}

その後、通常の方法でデータベースを使用できます。

于 2012-04-25T08:33:30.810 に答える
0

APK はまだ 50 MB に制限されているため、アプリケーションとは別に、拡張ファイルとしてデータベースを追加してみてください。詳細はこちら: http://android-developers.blogspot.com/2012/03/android-apps-break-50mb-barrier.html

于 2012-04-26T05:58:30.530 に答える
0

テーブルを作成するには、SQLiteOpenHelperここを参照し、そこにクレジットを記入してください)を使用できます

private class MyOpenHelper extends SQLiteOpenHelper {
    public MyOpenHelper(Context context)
    {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override public void onCreate(SQLiteDatabase db)
    {
       // Replace this SQL code with the code for your database.
        String query = "CREATE TABLE people (" +
          "_id integer primary key autoincrement not null, " +
          "first_name text, last_name text);";
        db.execSQL(query);
    }

    @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        // Called when the database needs to be upgraded. The implementation
        // should use this method to drop tables, add tables, or do anything
        // else it needs to upgrade to the new schema version.
    }
}

必要に応じて、アクセス時にテーブルが作成または更新されます。
テーブルの作成後に、必要に応じてデータベースにデータを入力することもできます。
APK のサイズを小さくするのに役立つ場合 (およびサイズが重要な場合) は、初期データをフィードする Web サービスを提供することをお勧めします。

于 2012-04-25T08:21:08.920 に答える