如何使用我自己的sqlite数据库? [英] How to use my own sqlite database?

查看:176
本文介绍了如何使用我自己的sqlite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把我的数据库字段放在assets文件夹中。并使用此博客将数据库复制到/ data / data / my_packname / databases /,(当我运行此应用程序时,此复制代码在onCreate()方法中运行),然后使用select * from ...获取数据。但它给了我的例外:没有这样的表。

I put my database field in "assets" folder. And use the code from this blog to copy the database to "/data/data/my_packname/databases/", (This copy code i run it in the onCreate() method when i run this app) then use select * from ... to get data. But it gives me the exception: no such table.

有人告诉我,如果我试图在SQLiteOpenHelper的onCreate()中复制文件,太晚了。因此,复制文件代码不能复制完整的文件。

Someone told me that if i am attempting to copy the file in SQLiteOpenHelper's onCreate(), it's too late. So the copy file code can not copy the complete file.

所以我需要使用adb或ddms来拉数据库?

So i need to use adb or ddms to pull the database first?

那么,任何人都可以教我如何使用我自己的数据库?
你能告诉我设置吗?

So, Anyone can teach me how to use my own databse? Can you tell me the setup?

推荐答案

我使用了那篇博文中的说明,而在正确的轨道,通过不必要地扩展 SQLiteOpenHelper 严重复杂的问题。我有更好的运气做以下:

I've used the instructions in that blog post and found them, while on the right track, to severely complicate the issue by unnecessarily extending SQLiteOpenHelper. I've had much better luck doing the following:


  1. 创建一个实用程序类,通过将静态数据库复制到

  1. Create a utility class that creates the static db by copying it into the correct directory from assets, but doesn't get itself so hung up on following the SQLiteOpenHelper format.

使用相同的实用程序类来打开数据库,方法是使用 SQLiteDatabase.openDatabase()

Using the same utility class to open the db by using SQLiteDatabase.openDatabase()

我创建的这个实用程序类;它不是很完整,但你会得到漂移。

Here is a version of this utility class I've created; it's not quite complete, but you'll get the drift.

public class DbUtils {
    private static final String DB_PATH = "/data/data/com.mypackage.myapp/databases/";
    private static final String DB_NAME = "my.db";

    public static void createDatabaseIfNotExists(Context context) throws IOException {
        boolean createDb = false;

        File dbDir = new File(DB_PATH);
        File dbFile = new File(DB_PATH + DB_NAME);
        if (!dbDir.exists()) {
            dbDir.mkdir();
            createDb = true;
        }
        else if (!dbFile.exists()) {
            createDb = true;
        }
        else {
            // Check that we have the latest version of the db
            boolean doUpgrade = false;

            // Insert your own logic here on whether to upgrade the db; I personally
            // just store the db version # in a text file, but you can do whatever
            // you want.  I've tried MD5 hashing the db before, but that takes a while.

            // If we are doing an upgrade, basically we just delete the db then
            // flip the switch to create a new one
            if (doUpgrade) {
                dbFile.delete();
                createDb = true;
            }
        }

        if (createDb) {
            // Open your local db as the input stream
            InputStream myInput = context.getAssets().open(DB_NAME);

            // Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(dbFile);

            // transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }
    }

    public static SQLiteDatabase getStaticDb() {
        return SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
    }
}

这篇关于如何使用我自己的sqlite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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