简体   繁体   English

使用sqliteassethelper的两个预打包数据库

[英]Two prepackaged databases using sqliteassethelper

So I am trying to prepackage a second database using SQLiteAssetHelper and am curious of the correct way to format the provider file. 因此,我试图使用SQLiteAssetHelper预先打包第二个数据库,并且对格式化提供程序文件的正确方法感到好奇。 I have already create my DatabaseHelper and all the additional files i need, but need to know how to get the second database created. 我已经创建了DatabaseHelper和所需的所有其他文件,但是需要知道如何创建第二个数据库。 Currently, my provider file looks as such: 当前,我的提供程序文件如下所示:

Provider.java Provider.java

/***
 Copyright (c) 2008-2012 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 From _The Busy Coder's Guide to Android Development_
 http://commonsware.com/Android
 */

package com.rcd.mypr.Workouts;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.provider.BaseColumns;
import android.text.TextUtils;

public class Provider extends ContentProvider {
    private static final int CONSTANTS = 1;
    private static final int CONSTANT_ID = 2;
    private static final UriMatcher MATCHER;
    private static final String TABLE = "constants";

    public static final class Constants implements BaseColumns {
        public static final Uri CONTENT_URI =
                Uri.parse("content://com.commonsware.android.constants.Provider/constants");
        public static final String DEFAULT_SORT_ORDER = "title";
        public static final String TITLE = "title";
        public static final String VALUE = "value";
    }

    static {
        MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
        MATCHER.addURI("com.commonsware.android.constants.Provider",
                "constants", CONSTANTS);
        MATCHER.addURI("com.commonsware.android.constants.Provider",
                "constants/#", CONSTANT_ID);
    }

    private WorkoutsDatabaseHelper db = null;
    private TheBenchmarkGirlsDatabaseHelper db2 = null;

    @Override
    public boolean onCreate() {
        db = new WorkoutsDatabaseHelper(getContext());
        db2 = new TheBenchmarkGirlsDatabaseHelper(getContext());

        return ((db == null) ? false : true);
    }

    @Override
    public Cursor query(Uri url, String[] projection, String selection,
                        String[] selectionArgs, String sort) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        qb.setTables(TABLE);

        String orderBy;

        if (TextUtils.isEmpty(sort)) {
            orderBy = Constants.DEFAULT_SORT_ORDER;
        } else {
            orderBy = sort;
        }

        Cursor c =
                qb.query(db.getReadableDatabase(), projection, selection,
                        selectionArgs, null, null, orderBy);

        c.setNotificationUri(getContext().getContentResolver(), url);

        return (c);
    }

    @Override
    public String getType(Uri url) {
        if (isCollectionUri(url)) {
            return ("vnd.commonsware.cursor.dir/constant");
        }

        return ("vnd.commonsware.cursor.item/constant");
    }

    @Override
    public Uri insert(Uri url, ContentValues initialValues) {
        long rowID =
                db.getWritableDatabase().insert(TABLE, Constants.TITLE,
                        initialValues);

        if (rowID > 0) {
            Uri uri =
                    ContentUris.withAppendedId(Provider.Constants.CONTENT_URI,
                            rowID);
            getContext().getContentResolver().notifyChange(uri, null);

            return (uri);
        }

        throw new SQLException("Failed to insert row into " + url);
    }

    @Override
    public int delete(Uri url, String where, String[] whereArgs) {
        int count = db.getWritableDatabase().delete(TABLE, where, whereArgs);

        getContext().getContentResolver().notifyChange(url, null);

        return (count);
    }

    @Override
    public int update(Uri url, ContentValues values, String where,
                      String[] whereArgs) {
        int count =
                db.getWritableDatabase()
                        .update(TABLE, values, where, whereArgs);

        getContext().getContentResolver().notifyChange(url, null);

        return (count);
    }

    private boolean isCollectionUri(Uri url) {
        return (MATCHER.match(url) == CONSTANTS);
    }
}

I stopped right around modifying the onCreate as I wasn't sure how to have it check if both or either db didnt exist and proceed correctly from there. 我不停地修改onCreate,因为我不确定如何检查它是否同时存在或不存在,并从那里正确进行。

Any insight would be appreciated! 任何见识将不胜感激!

Thanks 谢谢

EDIT: I just got to thinking about it, does it make more sense to just have a second table in the original database, rather than create an entirely new database? 编辑:我只是想一想,在原始数据库中仅拥有第二张表而不是创建一个全新的数据库是否更有意义?

Thanks! 谢谢!

Are you trying to use one database if the other doesn't exist and need to determine which one is present? 如果另一个数据库不存在,并且您需要确定存在哪个数据库,您是否正在尝试使用它? Other than that, in my provider, I have have multiple database helpers defined and determine which one to use based on the uri matcher. 除此之外,在我的提供程序中,我定义了多个数据库助手,并根据uri匹配器确定使用哪个。 I just use a bitmask so for example, 100x is dbHelper2 and 200x is dbHelper2. 我只是使用位掩码,因此,例如100x是dbHelper2,而200x是dbHelper2。 So you could do the following: 因此,您可以执行以下操作:

private static final int WORKOUT_CONSTANTS = 1001;
private static final int GIRLS_CONSTANTS = 2001;

private DbHelper getDbHelper(Uri uri)
{
    int uriCode = MATCHER.match(url);
    switch ((int)(uriCode / 1000))
    {
        case 1:
            return db1;
        case 2:
            return db2;
    }

    return null;
}

I just got to thinking about it, does it make more sense to just have a second table in the original database, rather than create an entirely new database? 我只是想一想,在原始数据库中仅拥有第二个表而不是创建一个全新的数据库是否更有意义?

Absolutely. 绝对。 A developer rarely needs more than one SQLite database directly. 开发人员很少直接需要多个SQLite数据库。 By "directly", I am specifically not counting the database that might be created by the use of a WebView widget, which happens "under the covers" from the standpoint of your application code. 通过“直接”,我特别不计算使用WebView小部件创建的数据库,从您的应用程序代码的角度来看,这是“在幕后”进行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM