获取 SQLite 数据库的下一个 AUTO_INCRMENT 值

发布于 2024-11-28 09:37:58 字数 211 浏览 4 评论 0原文

使用 Android API 中的典型 SQLiteDatabase 对象,我可以做什么来获取特定列(即 id)的下一个 AUTO_INCRMENT 值,而不影响该值本身。有办法吗?或者我应该执行什么查询才能获得该结果。请记住,SQLiteDatabase.query() 返回一个 Cursor 对象,因此如果我只想从中获取值,我不太确定如何直接处理它。

Using the typical SQLiteDatabase object in Android's API, what can I do to get the next AUTO_INCREMENT value of a particular column (ie. id) without affecting the value itself. Is there a method for that? Or what query should I execute to get that result. Keep in mind that SQLiteDatabase.query() returns a Cursor object, so I'm not too sure how to deal with that directly if I just want to get a value out of it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

反目相谮 2024-12-05 09:37:58

你说得对。第一个答案(仍在下面)仅在没有 id 的自动增量的情况下才有效。使用 AUTOINCRMENT,值存储在单独的表中并用于增量。这是查找值的示例:

public void printAutoIncrements(){
    String query = "SELECT * FROM SQLITE_SEQUENCE";
    Cursor cursor = mDb.rawQuery(query, null);
    if (cursor.moveToFirst()){
        do{
            System.out.println("tableName: " +cursor.getString(cursor.getColumnIndex("name")));
            System.out.println("autoInc: " + cursor.getString(cursor.getColumnIndex("seq")));

        }while (cursor.moveToNext());
    }

    cursor.close(); 

}

请参阅:http://www.sqlite.org/autoinc.html

第一个答案:

您可以查询 _id 列的最大值,例如:

String query = "SELECT MAX(id) AS max_id FROM mytable";
Cursor cursor = db.rawQuery(query, null);

int id = 0;     
if (cursor.moveToFirst())
{
    do
    {           
        id = cursor.getInt(0);                  
    } while(cursor.moveToNext());           
}
return id;

这适用于尚未指定为“INTEGER PRIMARY KEY AUTOINCRMENT”的行 id(所有表都有行 id 列)。

You're right. The first answer (still below) only works without an AUTOINCREMENT for id. With AUTOINCREMENT, the values are stored in a separate table and used for the increment. Here's an example of finding the value:

public void printAutoIncrements(){
    String query = "SELECT * FROM SQLITE_SEQUENCE";
    Cursor cursor = mDb.rawQuery(query, null);
    if (cursor.moveToFirst()){
        do{
            System.out.println("tableName: " +cursor.getString(cursor.getColumnIndex("name")));
            System.out.println("autoInc: " + cursor.getString(cursor.getColumnIndex("seq")));

        }while (cursor.moveToNext());
    }

    cursor.close(); 

}

See: http://www.sqlite.org/autoinc.html

First Answer:

You can query for the max of the _id column, such as:

String query = "SELECT MAX(id) AS max_id FROM mytable";
Cursor cursor = db.rawQuery(query, null);

int id = 0;     
if (cursor.moveToFirst())
{
    do
    {           
        id = cursor.getInt(0);                  
    } while(cursor.moveToNext());           
}
return id;

This works for row ids that haven't been specified as "INTEGER PRIMARY KEY AUTOINCREMENT" (all tables have a row id column).

心房的律动 2024-12-05 09:37:58

获取自动增量主键上最后一个 ID 的最佳方法

这是使用 SQLITE String query = "select seq from sqlite_sequence WHERE name = 'Table_Name'"

This is the best way to get the last ID on auto increment PRIMARY KEY with SQLITE

String query = "select seq from sqlite_sequence WHERE name = 'Table_Name'"

小…红帽 2024-12-05 09:37:58

关于 SQLITE_SEQUENCE 表的重要说明。

文档说

每当创建包含 AUTOINCREMENT 列的普通表时,都会自动创建并初始化 SQLITE_SEQUENCE 表。

因此,将创建 SQLITE_SEQUENCE 表,但不会创建与包含 AUTOINCREMENT 列的表关联的行。该行是使用第一个插入查询创建的(“seq”值为 1)。

这意味着在查找特定表的下一个自动增量值之前,您必须至少执行一次插入操作。例如,可以在创建表之后执行虚拟行的插入和删除。

An important remark about the SQLITE_SEQUENCE table.

The documentation says

The SQLITE_SEQUENCE table is created and initialized automatically whenever a normal table that contains an AUTOINCREMENT column is created.

So the SQLITE_SEQUENCE table is created, but NOT the row associated with the table that contains the AUTOINCREMENT column. That row is created with the first insert query (with "seq" value of 1).

That means that you must doing at least one insert operation before looking for the next autoincrement value of a specific table. It could be done for example just after the creation of the table, performing an insert and a delete of a dummy row.

爱*していゐ 2024-12-05 09:37:58

以下是我用来获取特定表的下一个 AUTOINCRMENT 值的方法:

/**
 * Query sqlite_sequence table and search for the AUTOINCREMENT value for <code>tableName</code>
 * @param tableName The table name with which the AUTOINCREMENT value is associated.
 *
 * @return The next AUTOINCREMENT value for <code>tableName</code>
 * If an INSERT call was not previously executed on <code>tableName</code>, the value 1 will
 * be returned. Otherwise, the returned value will be the next AUTOINCREMENT.
 */
private long getNextAutoIncrement(String tableName) {
    /*
     * From the docs:
     * SQLite keeps track of the largest ROWID 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.
     */
    String sqliteSequenceTableName = "sqlite_sequence";
    /*
     * Relevant columns to retrieve from <code>sqliteSequenceTableName</code>
     */
    String[] columns = {"seq"};
    String selection = "name=?";
    String[] selectionArgs = { tableName };

    Cursor cursor = mWritableDB.query(sqliteSequenceTableName, 
            columns, selection, selectionArgs, null, null, null);

    long autoIncrement = 0;

    if (cursor.moveToFirst()) {
        int indexSeq = cursor.getColumnIndex(columns[0]);
        autoIncrement = cursor.getLong(indexSeq);
    }

    cursor.close();

    return autoIncrement + 1;
}

Here is what I use to get the next AUTOINCREMENT value for a specific table:

/**
 * Query sqlite_sequence table and search for the AUTOINCREMENT value for <code>tableName</code>
 * @param tableName The table name with which the AUTOINCREMENT value is associated.
 *
 * @return The next AUTOINCREMENT value for <code>tableName</code>
 * If an INSERT call was not previously executed on <code>tableName</code>, the value 1 will
 * be returned. Otherwise, the returned value will be the next AUTOINCREMENT.
 */
private long getNextAutoIncrement(String tableName) {
    /*
     * From the docs:
     * SQLite keeps track of the largest ROWID 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.
     */
    String sqliteSequenceTableName = "sqlite_sequence";
    /*
     * Relevant columns to retrieve from <code>sqliteSequenceTableName</code>
     */
    String[] columns = {"seq"};
    String selection = "name=?";
    String[] selectionArgs = { tableName };

    Cursor cursor = mWritableDB.query(sqliteSequenceTableName, 
            columns, selection, selectionArgs, null, null, null);

    long autoIncrement = 0;

    if (cursor.moveToFirst()) {
        int indexSeq = cursor.getColumnIndex(columns[0]);
        autoIncrement = cursor.getLong(indexSeq);
    }

    cursor.close();

    return autoIncrement + 1;
}
一紙繁鸢 2024-12-05 09:37:58

在您使用的 SQLiteOpenHelper 内,启动一个事务。插入一些数据然后回滚。

这样,您就可以获得下一行 id,如下所示:

public long nextId() {
    long rowId = -1;

    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();
    try {
        ContentValues values = new ContentValues();
        // fill values ...

        // insert a valid row into your table
        rowId = db.insert(TABLE_NAME, null, values);

        // NOTE: we don't call  db.setTransactionSuccessful()
        // so as to rollback and cancel the last changes
    } finally {
        db.endTransaction();
    }
    return rowId;
}

Inside the SQLiteOpenHelper you use, start a transaction. Insert some data and then rollback.

Such a way, you 'll be able to get the next row id, like this:

public long nextId() {
    long rowId = -1;

    SQLiteDatabase db = getWritableDatabase();
    db.beginTransaction();
    try {
        ContentValues values = new ContentValues();
        // fill values ...

        // insert a valid row into your table
        rowId = db.insert(TABLE_NAME, null, values);

        // NOTE: we don't call  db.setTransactionSuccessful()
        // so as to rollback and cancel the last changes
    } finally {
        db.endTransaction();
    }
    return rowId;
}
清浅ˋ旧时光 2024-12-05 09:37:58

这是工作。

public static long getNextId(SQLiteDatabase db, String tableName) {
    Cursor c = null;
    long seq = 0;
    try {
        String sql = "select seq from sqlite_sequence where name=?";
        c = db.rawQuery(sql, new String[] {tableName});
        if (c.moveToFirst()) {
            seq = c.getLong(0);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return seq + 1;
}

It's work.

public static long getNextId(SQLiteDatabase db, String tableName) {
    Cursor c = null;
    long seq = 0;
    try {
        String sql = "select seq from sqlite_sequence where name=?";
        c = db.rawQuery(sql, new String[] {tableName});
        if (c.moveToFirst()) {
            seq = c.getLong(0);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return seq + 1;
}
爱要勇敢去追 2024-12-05 09:37:58

您可以使用 cursor.getInt(i); 方法
i 这里是 id 列的索引

Cursor c = db.rawQuery("Select * From mSignUp", null);
            String mail = null;
            try {
                while (c.moveToNext()) {
                    mail = c.getString(0);
                    String pas = c.getString(1);
                    Toast.makeText(getApplicationContext(), "Name = " + mail + " Pass = " + pas, Toast.LENGTH_SHORT).show();
                }
            }catch (CursorIndexOutOfBoundsException e){
                Log.e("OutOfBound", Log.getStackTraceString(e));
            }
            finally {
                c.close();
            }

You can use cursor.getInt(i); method
i here is index of the id column

Cursor c = db.rawQuery("Select * From mSignUp", null);
            String mail = null;
            try {
                while (c.moveToNext()) {
                    mail = c.getString(0);
                    String pas = c.getString(1);
                    Toast.makeText(getApplicationContext(), "Name = " + mail + " Pass = " + pas, Toast.LENGTH_SHORT).show();
                }
            }catch (CursorIndexOutOfBoundsException e){
                Log.e("OutOfBound", Log.getStackTraceString(e));
            }
            finally {
                c.close();
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文