Android 预填充数据库 [英] Android Pre-Populated Database

查看:26
本文介绍了Android 预填充数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

这是一个如何创建和填充数据库的示例,您可以在应用安装时执行此操作,但这只会创建一个条目,因此可能效率低下做你想做的事.

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
}

这篇关于Android 预填充数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆