从sqlite删除行无法正常工作

时间:2016-03-29 22:52:10

标签: android sqlite

我有一张表,我在插入数据时没有问题。但是当我尝试删除一行时,它返回0计数。它不会删除该行。我的sql语句出了什么问题?

表格创建

final String SQL_CREATE_DOG_TABLE = "CREATE TABLE " + DogContract.DogEntry.TABLE_NAME + " ( " +
                //identification
                DogContract.DogEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                DogContract.DogEntry.COLUMN_DOG_ID + " INTEGER NOT NULL, " +
                DogContract.DogEntry.COLUMN_DOG_TYPE + " TEXT NOT NULL, " +
                DogContract.DogEntry.COLUMN_OWNER_ID + " INTEGER, " +
                //position
                DogContract.DogEntry.COLUMN_BIRTH_TIME + " BIGINT NOT NULL, " +
                DogContract.DogEntry.COLUMN_DEATH_TIME + " BIGINT NOT NULL, " +
                //contact info
                DogContract.DogEntry.COLUMN_CONTACT_FIRSTNAME + " TEXT, " +
                DogContract.DogEntry.COLUMN_CONTACT_LASTNAME + " TEXT, " +
                DogContract.DogEntry.COLUMN_CONTACT_PHONE + " TEXT, " +
                //Dog data
                DogContract.DogEntry.COLUMN_FOODS + " TEXT, " +
                DogContract.DogEntry.COLUMN_TOYS + " TEXT, " +
                //address
                DogContract.DogEntry.COLUMN_ADDRESS_1 + " TEXT, " +
                DogContract.DogEntry.COLUMN_ADDRESS_2 + " TEXT, " +
                DogContract.DogEntry.COLUMN_CITY + " TEXT, " +
                DogContract.DogEntry.COLUMN_STATE + " TEXT, " +
                DogContract.DogEntry.COLUMN_ZIP_CODE + " TEXT" +
                ");";

        sqliteDatabase.execSQL(SQL_CREATE_DOG_TABLE);

删除查询

private int deleteByDogTypeAndId(Uri uri) {
    if (null == mOpenHelper) {
        mOpenHelper = new DogDBHelper(getContext());
    }
    final String TABLE_NAME = DogContract.DogEntry.TABLE_NAME;
    final String[] selectionArgs = DogContract.DogEntry.getDogTypeAndDogIdFromUri(uri);
    final String selection = DogContract.DogEntry.COLUMN_DOG_TYPE + " = ? AND " +
             DogContract.DogEntry.COLUMN_DOG_ID + " = ? ";
    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count = db.delete(TABLE_NAME, selection, selectionArgs);
    if (0 < count) {
        getContext().getContentResolver().notifyChange(DogContract.DogEntry.CONTENT_URI, null);
    }
    return count;
}

一些笔记。虽然我没有在这里显示日志,但我实际上是在记录这些步骤。 dogType是一个String而dogId是一个Long。请注意,dogId与dbId不同。 dogId在服务器上设置,而dbId是本地数据库构造。

0 个答案:

没有答案