在Android上创建一个SQLite数据库的最佳做法 [英] Best practices for creating a SQLite database on Android

查看:178
本文介绍了在Android上创建一个SQLite数据库的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经经历了很多的帖子上复制数据库中的资产或原始文件夹文件转移到 /数据/数据​​/ APP /数据库文件夹中阅读,但这将留下占用宝贵空间设备上的数据块的两个副本。我想建立一个解决方案,使用稍微更小的体积,并通过存储在原始文件夹中的数据库SQL创建文本文件,并使用标准的 on_create / on_update 在DBhelper级工艺。不过我有点困惑,因为复制数据库,通过旁路 on_create和on_update 方法的例子。

这是推荐的方法,如果你没有在code从字符串建立数据库?

我的解决方案将通过让所有在一个文件中的脚本模拟从code方法运行脚本。我期待在建设分贝这种方式的原因是,我的数据库将有接近100表申请完成的时候,所以我需要的模式是可控的。

任何指导,是值得欢迎的,因为我仍然在学习Android的最佳实践和模式。

下面是我的code的例子:

 公共类DatabaseHelper扩展SQLiteOpenHelper {
    私人最终字符串DATABASE_NAME =MYDB;
    私人最终诠释DATABASE_VERSION = 1;
    私人最终上下文myCtx;
    私人字符串DATABASE_CREATE_SCRIPT = NULL;    公共DatabaseHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
    }    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        DATABASE_CREATE_SCRIPT = getLoadFile();        //创建所有表和填充查找表
        db.execSQL(DATABASE_CREATE_SCRIPT);
        db.execSQL(VIEW_CREATE_V_PERSON);
    }    私人字符串getLoadFile(){
        为InputStream的InputStream = myCtx.getResources()openRawResource(resIdofmyfile)。        InputStreamReader的inputreader =新的InputStreamReader(InputStream的);
        的BufferedReader buffreader =新的BufferedReader(inputreader);
        串线;
        StringBuilder的文本=新的StringBuilder();        尝试{
        而((行= buffreader.readLine())!= NULL){
           text.append(线);
           text.append('\\ n');
         }
        }赶上(IOException异常五){
           //我们有一个错误查找
           返回null;
        }
        返回text.toString();
    }    / **
     * onUpgrade将检查数据库的版本和更新数据库
     *如果有新版本可用。
     * /
    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        //将设置条件检查升级
        //Log.w(TAG,+ oldVersion +到+ NEWVERSION +从版本升级数据库,它可以摧毁一切旧数据);
        db.execSQL(DROP TABLE IF EXISTS CONTEXT_LOAD);
            db.execSQL(DROP TABLE IF EXISTS问题);
            db.execSQL(DROP TABLE IF EXISTS目标);
            db.execSQL(DROP TABLE IF EXISTS人);            db.execSQL(DROP VIEW IF EXISTS V_PERSON);
        的onCreate(DB);
    }
}


解决方案

我真的不知道你的问题是什么。

如果你问它是否可以使用包含SQL CREATE语句那么答案是是。

加载一个文本文件,从头开始创建你的数据库

不仅如此,但它比分发更加灵活一个pre-内置APK的数据库内,你甚至可以从网络位置下载模式文件或文件。这使您可以在一个中心点动态更新的模式。

I have read through a lot of the posts on copying the database file over from the assets or raw folders to the /data/data/APP/databases folder, but that would leave two copies of the DB on the device taking up valuable space. I am thinking of building a solution with a slightly smaller footprint and allowing for more flexible schema management by storing the database SQL creation text file in the raw folder and using the standard on_create / on_update process in the DBhelper class. However I am a little confused because the examples that copy the database over bypass the on_create and on_update methods.

Is this the recommended way if you are not building the db from strings in the code?

My solution would simulate the running scripts from code method by having the scripts all in one file. The reason I am looking at building the db this way is that my DB will have close to 100 tables when the application is complete, so I need the schema to be manageable.

Any guidance is welcome as I am still learning the best practices and patterns for Android.

Here is an example of my code:

public class DatabaseHelper extends SQLiteOpenHelper {
    private final String DATABASE_NAME = "mydb";
    private final int DATABASE_VERSION = 1;
    private final Context myCtx;
    private String DATABASE_CREATE_SCRIPT = null;

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        DATABASE_CREATE_SCRIPT = getLoadFile();

        // Create all tables and populate the lookup tables
        db.execSQL(DATABASE_CREATE_SCRIPT);
        db.execSQL(VIEW_CREATE_V_PERSON);
    }

    private String getLoadFile(){
        InputStream inputStream = myCtx.getResources().openRawResource(resIdofmyfile);

        InputStreamReader inputreader = new InputStreamReader(inputStream);
        BufferedReader buffreader = new BufferedReader(inputreader);
        String line;
        StringBuilder text = new StringBuilder();

        try {
        while (( line = buffreader.readLine()) != null) {
           text.append(line);
           text.append('\n');
         }
        } catch (IOException e) {
           // We have an error to look up
           return null;
        }
        return text.toString();
    } 

    /**
     *  onUpgrade will check the version of the database and update the database 
     *  if a newer version is available.
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Will set conditional upgrade checks
        //Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which may destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS CONTEXT_LOAD");
            db.execSQL("DROP TABLE IF EXISTS ISSUES");
            db.execSQL("DROP TABLE IF EXISTS GOALS");
            db.execSQL("DROP TABLE IF EXISTS PERSON");

            db.execSQL("DROP VIEW IF EXISTS V_PERSON");
        onCreate(db);
    }


}

解决方案

I'm not really sure what your question is.

If you are asking if it's OK to create your DB from scratch using a text file containing a load of SQL CREATE statements then the answer is "yes".

Not only that but it's more flexible than distributing a pre-built DB within your APK as you could even download the 'schema' file or files from a network location. This allows you to update the schema dynamically at a central point.

这篇关于在Android上创建一个SQLite数据库的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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