I have researched this extensively and not been able to find an answer. I had a database working perfectly using the deprecated managecursor methods. Now i am trying to use cursor loader, and due to the requirement for content uri in the cursorloader query, I created a class that extends the ContentProvider class for inserting and querying the database. Now I am able to save to database, but after restarting the app, the database is not found (I think maybe it is being overwritten with a new empty database)
Here is the code:
DB class: public class CategoriesDB {
public static final String TABLE_CATEGORIES = "categories";
public static final String KEY_ROWID = "_id";
public static final String KEY_CATEGORY = "category";
private static final String CREATE_CATEGORIES_TABLE = "CREATE TABLE "
+ TABLE_CATEGORIES
+ "("
+ KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CATEGORY + " TEXT);";
public static void onCreate(SQLiteDatabase database) {
database.execSQL(CREATE_CATEGORIES_TABLE);
}
public static void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
Log.i(CategoriesDB.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS " + TABLE_CATEGORIES);
onCreate(database);
}
Helper class:
public class CategoriesDBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "categoriesDB.db";
private Context mCtx;
public CategoriesDBHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
mCtx = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
CategoriesDB.onCreate(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
CategoriesDB.onUpgrade(db, oldVersion, newVersion);
}
}
and ContentProvider class:
public class CategoryDBContentProvider extends ContentProvider {
private static final String AUTHORITY = "com.razi.gymlog.provider.CategoryDBContentProvider";
private static final String PATH = "categories";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/" + PATH);
private static final int CATEGORIES = 1;
private static final int CATEGORIES_ID = 2;
private static final UriMatcher sURIMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(AUTHORITY, PATH, CATEGORIES);
sURIMatcher.addURI(AUTHORITY, PATH + "/#", CATEGORIES_ID);
}
private CategoriesDBHelper database;
@Override
public boolean onCreate() {
database = new CategoriesDBHelper(getContext(), null, null, 1);
return false;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
int uriType = sURIMatcher.match(uri); // identify uri type (general to
// table, or specific to _id)
SQLiteDatabase sqlDB = database.getWritableDatabase();
long id = 0;
switch (uriType) {
case CATEGORIES:
Log.i("inserting", "inserting");
id = sqlDB
.insert(CategoriesDB.TABLE_CATEGORIES, null, values);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return Uri.parse(PATH + "/" + id);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = database.getWritableDatabase();
int rowsDeleted = 0;
switch (uriType) {
case CATEGORIES:
rowsDeleted = sqlDB.delete(CategoriesDB.TABLE_CATEGORIES,
selection,
selectionArgs);
break;
case CATEGORIES_ID:
String id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsDeleted = sqlDB.delete(CategoriesDB.TABLE_CATEGORIES,
CategoriesDB.KEY_ROWID + "=" + id,
null);
} else {
rowsDeleted = sqlDB.delete(CategoriesDB.TABLE_CATEGORIES,
CategoriesDB.KEY_ROWID + "=" + id
+ " and " + selection,
selectionArgs);
}
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsDeleted;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(CategoriesDB.TABLE_CATEGORIES);
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case CATEGORIES_ID:
queryBuilder.appendWhere(CategoriesDB.KEY_ROWID + "="
+ uri.getLastPathSegment());
break;
case CATEGORIES:
break;
default:
throw new IllegalArgumentException("Unknown URI");
}
Cursor cursor = queryBuilder.query(database.getReadableDatabase(),
projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
SQLiteDatabase sqlDB = database.getWritableDatabase();
int rowsUpdated = 0;
switch (uriType) {
case CATEGORIES:
rowsUpdated = sqlDB.update(CategoriesDB.TABLE_CATEGORIES, values,
selection, selectionArgs);
break;
case CATEGORIES_ID:
String id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsUpdated = sqlDB.update(CategoriesDB.TABLE_CATEGORIES, values,
CategoriesDB.KEY_ROWID + "=" + id, null);
} else {
rowsUpdated = sqlDB.update(CategoriesDB.TABLE_CATEGORIES, values,
CategoriesDB.KEY_ROWID + "=" + id + " and " + selection,
selectionArgs);
}
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsUpdated;
}
}
Any help appreciated, thanks!
Aucun commentaire:
Enregistrer un commentaire