使用 Contentprovider 删除行 ID

delete row id using Contentprovider

我正在使用提供程序删除 SQLiteOpenHelper 中的所有数据 table 假设我有 5 行并且我删除了 table 中的数据,如果我再次填充 table数据 _ID 列从 6

开始

SQLite TABLE

  final String HISTORY = "CREATE TABLE " + Contract.History.TABLE + " (" +
            Contract.History._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            Contract.History.ROW + " INTEGER NOT NULL, " +
            Contract.History.HISTORY_NAME + " TEXT NOT NULL " +
            " );";

删除供应商中的片段

 @Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    final SQLiteDatabase db = database.getWritableDatabase();
    final int match = matcher.match(uri);
    int rowsDeleted;
    if ( null == selection ) selection = "1";
    switch (match) {

        case HISTORY:
            rowsDeleted = db.delete(
                    Contract.History.TABLE, selection, selectionArgs);
            break;
        default:
            throw new UnsupportedOperationException("Unknown uri: " + uri);
    }
    // Because a null deletes all rows
    if (rowsDeleted != 0) {
        getContext().getContentResolver().notifyChange(uri, null);
    }
    return rowsDeleted;
}

我如何删除

 ContentResolver resolver = getContentResolver();
      resolver.delete(Contract.Running.CONTENT_URI,null,null);
    }

这是正常的数据库行为,不是代码或查询中的错误,而是 SQLite 的工作方式。 SQLite 站点状态:

SQLite keeps track of the largest ROWID that a table has ever held using an internal table named "sqlite_sequence". The sqlite_sequence table is created and initialized automatically whenever a normal table that contains an AUTOINCREMENT column is created. The content of the sqlite_sequence table can be modified using ordinary UPDATE, INSERT, and DELETE statements. But making modifications to this table will likely perturb the AUTOINCREMENT key generation algorithm. Make sure you know what you are doing before you undertake such changes.

我建议不要改变,除非你真的知道你在做什么。但如果你愿意,你可以这样做:

DELETE FROM sqlite_sequence WHERE name = '<YOUR_TABLE>';

您还可以更新排序器,以防您只删除一些行。

UPDATE SQLITE_SEQUENCE SET seq = <n> WHERE name = '<YOUR_TABLE>'