简体   繁体   中英

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. I have already create my DatabaseHelper and all the additional files i need, but need to know how to get the second database created. Currently, my provider file looks as such:

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.

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. I just use a bitmask so for example, 100x is dbHelper2 and 200x is 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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