每次启动应用程序时,是否真的需要创建SQLite表? [英] Is it really necessary to create SQLite tables every time the application starts?

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

问题描述

我在一个以上的SQLite教程中注意到,在扩展SQLiteOpenHelper的类的onCreate()事件中重新创建了表.我已经在Firefox加载项的帮助下在Android环境(Eclipse IDE)之外创建了SQLite数据库和表.数据库表位于预期位置:

I've noticed in more than one SQLite tutorial that the table is recreated in the onCreate() event of the class that extends SQLiteOpenHelper. I've already created my SQLite database and tables outside the Android environment (Eclipse IDE) with the help of a Firefox add-in. The database tables reside in the expected spot:

C:\aXX3&Space\Android\workspace\OnDemandAndAutomatic_Project\assets

在我看来,每次都必须以编程方式重新创建它们似乎很奇怪(显然,它们继续存在并保留数据,或者那是什么意思)?但是,此应用程序目前存在严重问题,这使我想到了以下问题:

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)? However, I'm having a serious issue with this application right now which leads me to my question:

  • 是否真的有必要每次都提醒Android有关数据库的信息?

令我非常恼火的是,我只是意识到,与其将数据库表放置在中,而不是在C:\aXX3&Space\Android\workspace\OnDemandAndAutomatic_Project\assets中创建数据库表,该版本是该应用程序的早期版本,但仍将SQLite文件复制到了正确的位置.一点都没改变.如果这是导致问题的原因,例如我的应用程序正在寻找AWOL数据库,仅复制并粘贴文件还不够吗?我必须做些什么来正式将数据库介绍到system/Android/Eclipse目录?有什么建议吗?

Much to my chagrin, I just realized that instead of creating my database tables in C:\aXX3&Space\Android\workspace\OnDemandAndAutomatic_Project\assets, I had put them in C:\aXX3&Space\Android\workspace\OnDemandAndAutomatic\assets, which was an earlier version of the application, yet copying the SQLite file to the proper location didn't change things a single bit. If this is what has been causing the problem, being that my application was looking for an AWOL database, is it not enough to simply copy and paste the file? Must I do something to officially introduce the database to the system/Android/Eclipse directory? Any suggestions?

推荐答案

实际上SQLiteOpenHelper负责数据库的创建,升级等等. 如果要以编程方式创建表,则必须在SQLiteOpenHelper类的onCreate方法中编写创建表查询.如果要在先前创建的数据库之后更新数据库,则只能在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.

如果您已经在外部创建了数据库,并且想要使用该数据库,则必须将该数据库放入资产文件夹中,然后将该文件复制到位于/data/data/packagename/databases文件夹中的数据库文件夹中.

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天全站免登陆