是不是真的有必要每次都创建的SQLite表格应用程序启动时? [英] Is it really necessary to create SQLite tables every time when app starts?

查看:140
本文介绍了是不是真的有必要每次都创建的SQLite表格应用程序启动时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在一个以上的SQLite的教程,该表为[重新]在扩展SQLiteOpenHelper类的OnCreate()事件创建。我已经创建了SQLite数据库和表了Android(Eclipse的)环境之外(使用Firefox的加载项)。在DB /表驻留在预期位置,我认为(C:\ aXX3和放大器;空间\机器人\工作区\ OnDemandAndAutomatic_Project \资产),而且看起来怪异,我认为我将不得不每次(显然,他们继续以编程方式重新创建它们存在,并保留数据,或会是什么点)?

I've noticed in more than one SQLite tutorial that the table is [re]created in the onCreate() event of the class that extends SQLiteOpenHelper. I've already created my SQLite database and tables outside of the Android (Eclipse) environment (with a Firefox add-in). The DB/Tables reside in the expected spot, I think (C:\aXX3&Space\Android\workspace\OnDemandAndAutomatic_Project\assets), and it seems bizarre to me that I would have to recreate them programmatically each time (obviously, they continue to exist, and retain data, or what would be the point)?

不过,我有这个程序一个严重的问题,所以我现在要问:难道真的让我不得不提醒的Andr​​oid有关的DB每dadblamed时间?说这是不是这样,乔!

However, I'm having a serious issue with this app right now SO I have to ask: Is it really so that I have to remind Android every dadblamed time about the DB? Say it ain't so, Joe!

更新: 令我懊恼的是,我才意识到,与其在ç创建我的数据库/表:\ aXX3和放大器;空间\机器人\工作区\ OnDemandAndAutomatic_Project \资产我把它们放在 C:\ aXX3和放大器;空间\机器人\工作区\ OnDemandAndAutomatic \资产(应用程序的早期版本)

Update: Much to my chagrin, I just realized that instead of creating my DB/Tables in C:\aXX3&Space\Android\workspace\OnDemandAndAutomatic_Project\assets I had put them in C:\aXX3&Space\Android\workspace\OnDemandAndAutomatic\assets (an earlier version of the app).

但复制的SQLite文件到合适的位置并没有改变的事情一点。如果这是已经造成的问题(我的应用程序在寻找一个AWOL数据库),是不是足够多简单地复制和粘贴文件?我必须做一些事情来正式介绍DB到系统/ Android的/ Eclipse的

Yet copying the SQLite file to the proper place didn't change things a bit. If this is what has been causing the problem (my app was looking for an AWOL database), is it not enought to simply copy and paste the file? Must I do something to "officially" introduce the DB to the system/Android/Eclipse?

推荐答案

实际上 SQLiteOpenHelper 负责数据库的创建,升级等等。 如果你想编程创建表,那么你必须写的onCreate方法创建一个表的查询中 SQLiteOpenHelper class.If要pviously更新$ P $后,你的数据库您可以编写修改表的查询在onUpgrade方法创建的数据库只有你将不得不更改数据库版本。

Actually SQLiteOpenHelper is responsible for your database creation, upgrade and many more. If you want to create tables programmatic then you have to write a create tables queries in the onCreate method of the SQLiteOpenHelper class.If you want to update your database after previously created database you can write the modified table's queries in the onUpgrade method only you will have to change the database version.

如果您已经创建了一个数据库外,如果你想使用该数据库,那么你必须把该数据库中的资产文件夹,复制其中的数据库文件夹中的文件驻留在 /数据/数据/包名称/数据库文件夹。

If you already created a database externally and if you want to use that database then you have to put that database in the assets folder and copy that file in the databases folder which is resides in the /data/data/packagename/databases folder.

这里是例子从资产复制数据库的数据库文件夹

private static boolean copyDataBase(Context c) throws IOException {
    String DB_PATH = "/data/data/" + c.getPackageName() + "/databases/";
    AssetManager mg = c.getResources().getAssets();
    InputStream myInput = null;

    try {
        myInput = mg.open(DATABASE_NAME);

    } catch (IOException ex) {
        return false;
    }

    if (myInput != null) {
        String outFileName = DB_PATH + DATABASE_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[8000];
        int length;

        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();

        Log.d(TAG, "Database is copied");
        return true;
    }
    return false;
}

下面是一个检查数据库和它的版本拷贝方法

public void copyDatabase() {

    try {
        SharedPreferences preferences = c.getSharedPreferences(c.getPackageName(), Context.MODE_PRIVATE);
        if (checkDataBase(c)) {
            if (preferences.getInt("dbversion", 0) != 0) {
                c.deleteDatabase(DatabaseHelper.DATABASE_NAME);
            }

        }
        getReadableDatabase();
        close();
        if (copyDataBase(c)) {
            Editor editor = preferences.edit();
            editor.putInt("dbversion", DatabaseHelper.DATABASE_VERSION);
            editor.commit();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

下面是一个检查气象数据库已经存在与否的例子?

public static boolean checkDataBase(Context c) {
    File f = new File("/data/data/" + c.getPackageName() + "/databases/"
            + DATABASE_NAME);
    if (!f.exists())
        return false;
    SQLiteDatabase checkDB = null;
    try {
        checkDB = SQLiteDatabase
                .openDatabase("/data/data/" + c.getPackageName()
                        + "/databases/" + DATABASE_NAME, null,
                        SQLiteDatabase.OPEN_READONLY);
        checkDB.close();
    } catch (SQLiteException e) {
        e.printStackTrace();
    }
    return checkDB != null ? true : false;
}

这篇关于是不是真的有必要每次都创建的SQLite表格应用程序启动时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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