如何在Android应用程序中使用我的自定义sqlite数据库?

时间:2015-06-27 22:27:56

标签: java android sqlite android-studio

我有一个静态的sqlite db。我怎么能把它包含在应用程序中?我应该把它放在我的项目文件夹中?我应该如何从DatabaseHandler访问它?

我在网上找到的所有东西都只使用sqlite创建一个新的数据库并在其中存储用户或临时数据,但不使用带有预定义数据的现有数据库。

Official Google docs没有说明如何做到这一点。

1 个答案:

答案 0 :(得分:0)

处理此案例基本上只是进行文件复制。

棘手的部分是

  • 仅在需要时创建数据库(否​​则只需打开它)
  • 实施升级逻辑

我写了一个示例Helper类,演示了如何从资产中加载数据库。

public abstract class SQLiteAssetHelper extends SQLiteOpenHelper {

    // ----------------------------------
    // CONSTANTS
    // ----------------------------------

    private static final String DATABASE_DIR_NAME = "databases";

    // ----------------------------------
    // ATTRIBUTES
    // ----------------------------------

    private final Context mContext;
    private final CursorFactory mFactory;

    private SQLiteDatabase mDatabase;

    private String mDatabaseName;
    private String mDatabaseAssetPath;
    private String mDatabaseDiskPath;

    private boolean mIsProcessingDatabaseCreation; // Database creation may take some time

    // ----------------------------------
    // CONSTRUCTORS
    // ----------------------------------

    public SQLiteAssetHelper(Context context, String name, CursorFactory factory, String destinationPath, int version) {
        super(context, name, factory, version);

        mContext = context;
        mFactory = factory;

        mDatabaseName = name;

        mDatabaseAssetPath = DATABASE_DIR_NAME + "/" + name;
        if (destinationPath == null) {
            mDatabaseDiskPath = context.getApplicationInfo().dataDir + "/" + DATABASE_DIR_NAME;
        } else {
            mDatabaseDiskPath = destinationPath;
        }
    }

    // ----------------------------------
    // OVERRIDEN METHODS
    // ----------------------------------

    @Override
    public synchronized SQLiteDatabase getWritableDatabase() {
        if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
            // the database is already open and writable
            return mDatabase;
        }

        if (mIsProcessingDatabaseCreation) {
            throw new IllegalStateException("getWritableDatabase is still processing");
        }

        SQLiteDatabase db = null;
        boolean isDatabaseLoaded = false;

        try {
            mIsProcessingDatabaseCreation = true;
            db = createOrOpenDatabase();
            // you should probably check for database new version and process upgrade if necessary
            onOpen(db);
            isDatabaseLoaded = true;
            return db;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            mIsProcessingDatabaseCreation = false;
            if (isDatabaseLoaded) {
                if (mDatabase != null) {
                    try {
                        mDatabase.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                mDatabase = db;
            } else {
                if (db != null) db.close();
            }
        }
    }

    @Override
    public final void onCreate(SQLiteDatabase db) {
        // getWritableDatabase() actually handles database creation so nothing to code here
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO implement your upgrade logic here
    }

    // ----------------------------------
    // PRIVATE METHODS
    // ----------------------------------

    private void copyDatabaseFromAssets() throws IOException {

        String dest = mDatabaseDiskPath + "/" + mDatabaseName;
        String path = mDatabaseAssetPath;

        InputStream is = mContext.getAssets().open(path);

        File databaseDestinationDir = new File(mDatabaseDiskPath + "/");
        if (!databaseDestinationDir.exists()) {
            databaseDestinationDir.mkdir();
        }
        IOUtils.copy(is, new FileOutputStream(dest));
    }

    private SQLiteDatabase createOrOpenDatabase() throws IOException {

        SQLiteDatabase db = null;
        File file = new File (mDatabaseDiskPath + "/" + mDatabaseName);
        if (file.exists()) {
            db = openDatabase();
        }

        if (db != null) {
            return db;
        } else {
            copyDatabaseFromAssets();
            db = openDatabase();
            return db;
        }
    }

    private SQLiteDatabase openDatabase() {
        try {
            SQLiteDatabase db = SQLiteDatabase.openDatabase(
                    mDatabaseDiskPath + "/" + mDatabaseName, mFactory, SQLiteDatabase.OPEN_READWRITE);
            return db;
        } catch (SQLiteException e) {
            e.printStackTrace();
            return null;
        }
    }

    // ----------------------------------
    // NESTED CLASSES
    // ----------------------------------

    private static class IOUtils {

        private static final int BUFFER_SIZE = 1024;

        public static void copy(InputStream in, OutputStream outs) throws IOException {
            int length;
            byte[] buffer = new byte[BUFFER_SIZE];

            while ((length = in.read(buffer)) > 0) {
                outs.write(buffer, 0, length);
            }

            outs.flush();
            outs.close();
            in.close();
        }

    }; // IOUtils

}

然后你只需要创建一个类似于上面的类:

public class MyDbHelper extends SQLiteAssetHelper {

    // ----------------------------------
    // CONSTANTS
    // ----------------------------------

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_FILE_NAME = "test.db";

    // ----------------------------------
    // CONSTRUCTORS
    // ----------------------------------

    public MyDbHelper(Context context) {
        super(context, DATABASE_FILE_NAME, null, context.getFilesDir().getAbsolutePath(), DATABASE_VERSION);
    }

}

每次从MyDbHelper实例调用getWritableDatabase()时,它都会为您执行所有复制/打开的操作并返回可写数据库。

正如我之前所说,我没有在此示例中实现upgrade()方法,您必须这样做。 我也没有实现getReadableDatabase(),因为我通常只使用getWritableDatabase()。你可能需要这样做。

如果您想测试它,请执行以下操作:

  • 复制上面的代码
  • 在您的资产中创建一个名为" databases"的文件夹。并在其中插入sqlite数据库文件
  • 在MyDatabaseHelper中,使用资产文件夹中的数据库名称更改DATABASE_FILE_NAME常量的值
  • 不要忘记实例化MyDatabaseHelper并调用getWritableDatabse()

希望这会有所帮助。