簡體   English   中英

使用sqliteassethelper的兩個預打包數據庫

[英]Two prepackaged databases using sqliteassethelper

因此,我試圖使用SQLiteAssetHelper預先打包第二個數據庫,並且對格式化提供程序文件的正確方法感到好奇。 我已經創建了DatabaseHelper和所需的所有其他文件,但是需要知道如何創建第二個數據庫。 當前,我的提供程序文件如下所示:

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);
    }
}

我不停地修改onCreate,因為我不確定如何檢查它是否同時存在或不存在,並從那里正確進行。

任何見識將不勝感激!

謝謝

編輯:我只是想一想,在原始數據庫中僅擁有第二張表而不是創建一個全新的數據庫是否更有意義?

謝謝!

如果另一個數據庫不存在,並且您需要確定存在哪個數據庫,您是否正在嘗試使用它? 除此之外,在我的提供程序中,我定義了多個數據庫助手,並根據uri匹配器確定使用哪個。 我只是使用位掩碼,因此,例如100x是dbHelper2,而200x是dbHelper2。 因此,您可以執行以下操作:

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;
}

我只是想一想,在原始數據庫中僅擁有第二個表而不是創建一個全新的數據庫是否更有意義?

絕對。 開發人員很少直接需要多個SQLite數據庫。 通過“直接”,我特別不計算使用WebView小部件創建的數據庫,從您的應用程序代碼的角度來看,這是“在幕后”進行的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM