所以我试图使用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时停下来,因为我不确定如何检查是否存在这两个或者任何一个db都不存在并从那里正确进行。
任何见解都将受到赞赏!
由于
编辑:我只是想一想,在原始数据库中只有第二个表更有意义,而不是创建一个全新的数据库吗?谢谢!
答案 0 :(得分:0)
如果另一个数据库不存在并且需要确定哪个数据库存在,您是否尝试使用该数据库?除此之外,在我的提供程序中,我已经定义了多个数据库帮助程序,并根据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;
}
答案 1 :(得分:0)
我只是想一想,在原始数据库中只有第二个表更有意义,而不是创建一个全新的数据库吗?
绝对。开发人员很少直接需要多个SQLite数据库。通过"直接",我特别没有计算可能通过使用WebView
小部件创建的数据库,这种情况发生在"封面下#34;从您的应用程序代码的角度来看。