Android 预填充数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2409126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:45:49  来源:igfitidea点击:

Android Pre-Populated Database

databaseandroid

提问by Blather

I am working on an Android application that will need several entries (a single table, with 1000-10000 rows) populated in that app's database before the user can use that app. I've looked around some tutorials and I am unsure of the best way to do this. Should I just check if the database exists each time the app is started and, if it isn't there, create it and insert the thousands of records I need? Or is there a better way to handle this problem? Ideally, it could be included as part of the app's install process, but I'm not sure if this is possible. Any feedback would be greatly appreciated.

我正在开发一个 Android 应用程序,在用户可以使用该应用程序之前,该应用程序需要在该应用程序的数据库中填充多个条目(一个表,具有 1000-10000 行)。我环顾了一些教程,但不确定执行此操作的最佳方法。我是否应该在每次启动应用程序时检查数据库是否存在,如果不存在,则创建它并插入我需要的数千条记录?或者有没有更好的方法来处理这个问题?理想情况下,它可以作为应用程序安装过程的一部分,但我不确定这是否可行。任何反馈将不胜感激。

回答by moritz

the way I'm going here is to ship a prepopulated database in the assets folder. You can drop in files there and use them as-they-are. Beware, however, that there is a size limit of 1MB, so maybe you'll have to split files, or compress them.

我要在这里的方式是在资产文件夹中发送一个预填充的数据库。您可以将文件放入那里并按原样使用它们。但是请注意,有 1MB 的大小限制,因此您可能必须拆分文件或压缩它们。

Compression is quite handy and well supported by the os itself.

压缩非常方便,并且由操作系统本身提供了很好的支持。

hope this was of any help :-)

希望这有帮助:-)

回答by Donal Rafferty

Here is an example of how to create and populate a database, you can just do this on the app install, this only creates one entry though so may be inefficient for what you want to do.

这是如何创建和填充数据库的示例,您可以在应用程序安装时执行此操作,这只会创建一个条目,但对于您想要执行的操作可能效率低下。

private static class settingsDatabaseHelper extends SQLiteOpenHelper{

    //SQL String for creating the table required
    private static final String CREATE_SETTINGS_TABLE
    = "CREATE TABLE tbl_settings(" +
            "_ID INTEGER PRIMARY KEY AUTOINCREMENT," +
            "VOIPUSERNAME TEXT," +
            "VOIPAUTHID TEXT," +
            "PASSWORD TEXT," +
            "VOIPDISPLAYNAME TEXT," +
            "SIPPROXYSERVER TEXT," +
            "SIPREGISTRAR TEXT," +
            "SIPREALM TEXT," +
            "EXPIRESTIME INTEGER);";    

    //constructor
    public settingsDatabaseHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_SETTINGS_TABLE);
         ContentValues initialValues = new ContentValues();
            initialValues.put("VOIPUSERNAME", "xxxxx");
            initialValues.put("VOIPAUTHID", "xxxxxxxxxx");
            initialValues.put("PASSWORD", "xxxxxx");
            initialValues.put("VOIPDISPLAYNAME", "xxxxxxxxx");
            initialValues.put("SIPPROXYSERVER", "xxxxxxxxxxxxx");
            initialValues.put("SIPREGISTRAR", "xxxxxxxxxxx");
            initialValues.put("SIPREALM", "xxxxxxxxxx");
            initialValues.put("EXPIRESTIME", xxxxxxxxxxx);
            Log.d("1.6", "gets to here");
            db.insert(SETTINGS_TABLE, null, initialValues);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " +
                 newVersion + ", which will destroy all old data");

        db.execSQL("DROP TABLE IF EXISTS " + SETTINGS_TABLE);
        onCreate(db);

    } 

}

//end helper class
}

回答by WarrenFaith

JavaDoc from SQLiteOpenHelper:

来自 SQLiteOpenHelper 的 JavaDoc:

A helper class to manage database creation and version management. You create a subclass implementing onCreate(SQLiteDatabase), onUpgrade(SQLiteDatabase, int, int) and optionally onOpen(SQLiteDatabase), and this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary. Transactions are used to make sure the database is always in a sensible state.

For an example, see the NotePadProvider class in the NotePad sample application, in the samples/ directory of the SDK.

用于管理数据库创建和版本管理的助手类。您创建一个实现 onCreate(SQLiteDatabase)、onUpgrade(SQLiteDatabase, int, int) 和可选的 onOpen(SQLiteDatabase) 的子类,该类负责打开数据库(如果存在),如果不存在则创建它,并根据需要对其进行升级. 事务用于确保数据库始终处于合理状态。

有关示例,请参阅 SDK 的 samples/ 目录中的 NotePad 示例应用程序中的 NotePadProvider 类。

So if you extend this class, you have 3 methods which will be called in some cases and you can choose, what do to. Thats the best practice :)

所以如果你扩展这个类,你有 3 个方法,在某些情况下会被调用,你可以选择做什么。那是最好的做法:)