通过ContentProvider更新SQLite数据库中的特定列的问题

时间:2016-03-21 08:26:38

标签: java android sqlite sql-update

我正在尝试更新表格中特定列的值。我已经设置了ContentProvder。我收到错误,但我不知道如何修复它。我在网上搜索过帮助,似乎没有一个基于unique-id更新。这是我的代码..

@Override
public int update (Uri uri, ContentValues values, String selection,
                   String[] selectionArgs) {
    int uriType = NewsContract.sURI_MATCHER.match(uri);
    SQLiteDatabase db = dictionaryHelper.getWritableDatabase();
    int rowsUpdated = 0;

    String id = null;
    switch (uriType)
    {case DictionaryContract.RECENT:
            rowsUpdated = db.update(DictionaryContract.DictionaryDataContract.RECENT_TABLE_NAME,
                    values,
                    selection,
                    selectionArgs);
            break;
        case DictionaryContract.RECENT_ID:
            id = uri.getLastPathSegment();
            if (TextUtils.isEmpty(selection)) {
                rowsUpdated = db.update(DictionaryContract.DictionaryDataContract.RECENT_TABLE_NAME,
                        values,
                        DictionaryContract.DictionaryDataContract._ID + "=" + id,
                        null);
            } else {
                rowsUpdated = db.update(DictionaryContract.DictionaryDataContract.RECENT_TABLE_NAME,
                        values,
                        DictionaryContract.DictionaryDataContract._ID + "=" + id
                                + " and "
                                + selection, selectionArgs);
            }
            break;
        default:
            throw new IllegalArgumentException("Unknown URI" + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);

    return rowsUpdated;
}

这就是我调用ContentProvider

的更新方法的方法
private boolean UpdateRecentTable()
{
    cursor.moveToLast();
    Long id = cursor.getLong(cursor.getColumnIndex(DictionaryContract.DictionaryDataContract._ID));
    Log.i("Update", " " + id);
    String filter = DictionaryContract.DictionaryDataContract._ID + "=" + id;
    ContentValues args = new ContentValues();
    args.put(DictionaryContract.DictionaryDataContract.REC_FAVORITED, 1);
    dResolver.update(DictionaryContract.CONTENT_URI_RECENT, args, filter, null);
    return true;
}

错误:

03-21 04:05:36.823 18326-18673/com.example.clinton.light E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.example.clinton.light, PID: 18326
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalArgumentException: Unknown URIcontent://com.example.clinton.light.provider.dictionary/recents
at com.example.clinton.light.database.DictionaryContentProvider.update(DictionaryContentProvider.java:216)
at android.content.ContentProvider$Transport.update(ContentProvider.java:355)
at android.content.ContentResolver.update(ContentResolver.java:1362)
at com.example.clinton.light.dictionary_main.StoreFavWord.UpdateRecentTable(StoreFavWord.java:72)
at com.example.clinton.light.dictionary_main.StoreFavWord.doInBackground(StoreFavWord.java:41)
at com.example.clinton.light.dictionary_main.StoreFavWord.doInBackground(StoreFavWord.java:18)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 
03-21 04:05:37.271 18326-18352/com.example.clinton.light E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe0c79230

DataContract

ublic class DictionaryContract {
public DictionaryContract(){}

public static final int TODAY = 100;
public static final int TODAY_ID = 115;
public static final int FAVORITE = 120;
public static final int FAVORITE_ID = 125;
public static final int RECENT = 130;
public static final int RECENT_ID = 135;

private static final String AUTHORITY = "com.example.clinton.light.provider.dictionary";
public static final String TODAY_BASE_PATH = "today";
public static final String FAVORITE_BASE_PATH = "favorites";
public static final String RECENT_BASE_PATH = "recents";

public static final UriMatcher sURI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

static {
    sURI_MATCHER.addURI(AUTHORITY, TODAY_BASE_PATH, TODAY);
    sURI_MATCHER.addURI(AUTHORITY, TODAY_BASE_PATH + "/#", TODAY_ID);
    sURI_MATCHER.addURI(AUTHORITY, FAVORITE_BASE_PATH, FAVORITE);
    sURI_MATCHER.addURI(AUTHORITY, FAVORITE_BASE_PATH + "/#", FAVORITE_ID);
    sURI_MATCHER.addURI(AUTHORITY, RECENT_BASE_PATH, RECENT);
    sURI_MATCHER.addURI(AUTHORITY, RECENT_BASE_PATH + "/#", RECENT_ID);
}

public static final Uri CONTENT_URI_TODAY = Uri.parse("content://" + AUTHORITY + "/" + TODAY_BASE_PATH);
public static final Uri CONTENT_URI_FAVORITES = Uri.parse("content://" + AUTHORITY + "/" + FAVORITE_BASE_PATH);
public static final Uri CONTENT_URI_RECENT = Uri.parse("content://" + AUTHORITY + "/" + RECENT_BASE_PATH);

public static abstract class DictionaryDataContract implements BaseColumns
{
    public static final String TODAY_TABLE_NAME = "todaytable";
    public static final String TODAY_DATE = "worddate";
    public static final String WORD = "wordname";
    public static final String DEFINITION = "worddefinition";
    public static final String SPEECH = "wordspeech";
    public static final String ROOT = "wordroot";
    public static final String EXAMPLE = "wordexample";

    private static final String CREATE_TODAY_TABLE = "create table "+
            TODAY_TABLE_NAME +
            "(" +
            _ID +
            " integer primary key autoincrement, " +
            TODAY_DATE +
            " text not null, " +
            WORD +
            " text not null, " +
            DEFINITION +
            " text not null, " +
            EXAMPLE +
            " text not null, " +
             SPEECH +
            " text not null, " +
            ROOT +
            " text not null);";


    public static final String FAVORITE_TABLE_NAME = "favoritetable";
    public static final String FAV_WORD = "wordname";
    public static final String FAV_DEFINITION = "worddefinition";
    public static final String FAV_DEFINITION2 = "worddefinition2";
    public static final String FAV_SPEECH = "wordspeech";
    public static final String FAV_SPEECH2 = "wordspeech2";
    public static final String FAV_RELATED = "wordrelated";
    public static final String FAV_EXAMPLE = "wordexample";

    public static final String RECENT_TABLE_NAME = "recentetable";
    public static final String REC_WORD = "wordname";
    public static final String REC_DEFINITION = "worddefinition";
    public static final String REC_DEFINITION2 = "worddefinition2";
    public static final String REC_SPEECH = "wordspeech";
    public static final String REC_SPEECH2 = "wordspeech2";
    public static final String REC_RELATED = "wordrelated";
    public static final String REC_EXAMPLE = "wordexample";
    public static final String REC_FAVORITED = "favorited";

    private static final String CREATE_FAVORITE_TABLE = "create table "+
            FAVORITE_TABLE_NAME +
            "(" +
            _ID +
            " integer primary key autoincrement, " +
            FAV_WORD +
            " text not null, " +
            FAV_DEFINITION +
            " text not null, " +
            FAV_DEFINITION2 +
            " text not null, " +
            FAV_EXAMPLE +
            " text not null, " +
            FAV_SPEECH +
            " text not null, " +
            FAV_SPEECH2 +
            " text not null, " +
            FAV_RELATED +
            " text not null);";

    private static final String CREATE_RECENT_TABLE = "create table "+
            RECENT_TABLE_NAME +
            "(" +
            _ID +
            " integer primary key autoincrement, " +
            REC_WORD +
            " text not null, " +
            REC_DEFINITION +
            " text not null, " +
            REC_DEFINITION2 +
            " text not null, " +
            REC_EXAMPLE +
            " text not null, " +
            REC_SPEECH +
            " text not null, " +
            REC_SPEECH2 +
            " text not null, " +
            REC_FAVORITED +
            " int not null, " +
            REC_RELATED +
            " text not null);";

    public static void onCreate (SQLiteDatabase db) {
        db.execSQL(CREATE_TODAY_TABLE);
        db.execSQL(CREATE_FAVORITE_TABLE);
        db.execSQL(CREATE_RECENT_TABLE);

    }

    public static void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TODAY_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + FAVORITE_TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS " + RECENT_TABLE_NAME);
        onCreate(db);
    }
}

}

0 个答案:

没有答案