Hello,
I have 5 Content Providers for my 5 table SQLiteDatabase. They are
named:
CoreActionProvider
CoreMovementProvider
CoreRoundsProvider
CoreChecksProvider
CoreTermsProvider
I have stepped through the startup process several times and have
confirmed that all 5 Providers are loading. However, only
CoreTermsProvider and CoreChecksProvider are firing their onCreate()
methods.
The tables do not exist for for the other 3. I have attempted to run
queries against them, and been greeted with the dreaded SQLException,
stating that the table does not exist.
Is there a limit to the number of custom providers?
I will post the code for CoreTermsProvider only, as all 5 providers
share the same code structure, just different table names:
package com.vortex.rules.providers;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
public class CoreTermsProvider extends ContentProvider{
public static final String PROVIDER_NAME =
"com.vortex.rules.providers.CoreTerms";
public static final Uri CONTENT_URI = Uri.parse("content://"+
PROVIDER_NAME + "/terms");
public static final String _ID = "_id";
public static final String TITLE = "title";
public static final String TEXT = "text";
private static final int TERMS = 1;
private static final int TERM_ID = 2;
private static final UriMatcher uriMatcher;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "terms", TERMS);
uriMatcher.addURI(PROVIDER_NAME, "terms/#", TERM_ID);
}
//---for database use---
private SQLiteDatabase rulesDB;
private static final String DATABASE_NAME = "Rules.db";
private static final String DATABASE_TABLE = "terms";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table if not exists " + DATABASE_TABLE +
"(_id integer primary key autoincrement, "
+ "title text not null, text longtext not null);";
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion){
Log.w("Content Provider Database",
"Upgrading database from version " +
oldVersion + " to " + newVersion +
", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS terms");
onCreate(db);
}
}
@Override
public String getType(Uri uri){
switch (uriMatcher.match(uri)){
//---get all terms---
case TERMS:
return "vnd.android.cursor.dir/vnd.vortex.rules.terms ";
//---get a particular book
case TERM_ID:
return "vnd.android.cursor.item/vnd.vortex.rules.terms
";
default:
throw new IllegalArgumentException("Unsupported URI: "
+ uri);
}
}
@Override
public boolean onCreate(){
Context context = this.getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
rulesDB = dbHelper.getWritableDatabase();
return (rulesDB == null)? false:true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder){
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
sqlBuilder.setTables(DATABASE_TABLE);
if (uriMatcher.match(uri) == TERM_ID)
//---if getting a particular term---
sqlBuilder.appendWhere(_ID + "=" +
uri.getPathSegments().get(1));
if (sortOrder == null || sortOrder=="")
sortOrder = TITLE;
Cursor c = sqlBuilder.query(rulesDB, projection, selection,
selectionArgs, null, null, sortOrder);
//---register to watch a content URI for changes---
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public Uri insert(Uri uri, ContentValues values){
//---add a new term---
long rowId = rulesDB.insert(DATABASE_TABLE, "", values);
//---if added successfully---
if (rowId > 0)
{
Uri _uri = ContentUris.withAppendedId(CONTENT_URI,
rowId);
getContext().getContentResolver().notifyChange(_uri,
null);
return _uri;
}
throw new SQLException("Failed to insert row into " + uri);
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2){
// arg0 = uri
// arg1 = selection
// arg2 = se;ectionArgs
int count = 0;
switch (uriMatcher.match(arg0)) {
case TERMS:
count = rulesDB.delete(DATABASE_TABLE, arg1, arg2);
break;
case TERM_ID:
String id = arg0.getPathSegments().get(1);
count = rulesDB.delete(DATABASE_TABLE, _ID + "=" + id +
(!TextUtils.isEmpty(arg1)? " AND (" +
arg1 + ')' : ""), arg2);
break;
default:
throw new IllegalArgumentException("Unknown URI: " +
arg0);
}
getContext().getContentResolver().notifyChange(arg0, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values,
String selection, String[] selectionArgs)
{
int count = 0;
switch (uriMatcher.match(uri)){
case TERMS:
count = rulesDB.update(DATABASE_TABLE, values,
selection,
selectionArgs);
break;
case TERM_ID:
count = rulesDB.update(DATABASE_TABLE, values,
_ID + "=" +
uri.getPathSegments().get(1) +
(!TextUtils.isEmpty(selection)
? " AND (" +
selection + ')'
: ""), selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown
URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
As you can see, it is a basic Content Provider.
Any help on this issue would be greatly appreciated. I would have
posted this on StackOverflow, but the "Ask A Question" link was
throwing errors.
Sincerely,
Michael Martin
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en