sqlite插入自增长标识字段,获得新id

在sqlite中

insert into t_1(id,name)values(NULL,'赵玉开5');
select last_insert_rowid() newid;

注意获得新id的函数是last_insert_rowid()而不是last_insert_id(),last_insert_id()函数是mysql中获得新插入自增数字主键的函数。另外sqlite比较怪异的一点是,对于自增数字主键,必须指定为NULL值。

 

rowid和 Integer主键及自增属性

大多数情况下,sqlite3的表都有一个rowid(也叫oid,_rowid_),这是一个64位的整数,并作为sqlite存储结构B树的主键.因此使用rowid查询会比以其他设定的主键查询,速度会非常快.

在做插入操作的时候,对于rowid的值通常情况下不要去指定,让系统自己去决定该去何值。因为sqlite会通过SQLITE_SEQUENCE来追踪表的rowid取值情况.而且sqlite定义了rowid的取值算法:在未超出rowid的范围内,待插入记录的rowid总是表中存在过的的rowid最大值+1。比如依次插入5条记录,此时最后一条记录的rowid是5,如果把这条记录删除再插入新记录,此时新纪录的rowid是6.而当rowid达到所能表达的最大值时,这时如果有新纪录要插入,系统就会随机从之前的没有使用过的正整数中随机取一个作为rowid(就是之前删除过的).若没有未使用的正整数并且你没有在插入的时候制定rowid为某一个负数的话,系统就会抛出SQLITE_FULL的错误.

如果在创建表的时候设置了主键,并且设置主键的那列是integer(不是int,short integer等等),并且主键没有设定降序时,这时的主键是rowid的别名,换言之,主键和rowid没有区别.如果我们再设定主键autoincrement属性时,和rowid又有什么区别呢?区别只是在于此时主键的取值是一个未使用过的rowid值,而这个rowid值系统会保证其是单调增长的,通常情况下就是表中存在过的rowid最大值+1.这里所说的通常情况下是因为有这样一种情况:当插入操作由于表约束而失败的时候,本来要赋值的rowid,有可能不会在下次插入操作中使用,此时主键的取值就是表中存在过的rowid最大值+2.

因此对于用户id是整数的表,是单独设主键去维护还是直接使用rowid作为主键,就取决于各自的业务逻辑关系了.在这种情况下,通常不使用rowid的主键特性.

 

方法一:

如果是下面这情形,可以用这个方法的返回值。the row ID of the newly inserted row, or -1 if an error occurred 

情形:如果在创建表的时候设置了主键,并且设置主键的那列是integer(不是int,short integer等等),并且主键没有设定降序时,这时的主键是rowid的别名

 /**
     * Convenience method for inserting a row into the database.
     *
     * @param table the table to insert the row into
     * @param nullColumnHack optional; may be <code>null</code>.
     *            SQL doesn't allow inserting a completely empty row without
     *            naming at least one column name.  If your provided <code>values</code> is
     *            empty, no column names are known and an empty row can't be inserted.
     *            If not set to null, the <code>nullColumnHack</code> parameter
     *            provides the name of nullable column name to explicitly insert a NULL into
     *            in the case where your <code>values</code> is empty.
     * @param values this map contains the initial column values for the
     *            row. The keys should be the column names and the values the
     *            column values
     * @return the row ID of the newly inserted row, or -1 if an error occurred
     */

public long insert(String table, String nullColumnHack, ContentValues values) {
        try {
            return insertWithOnConflict(table, nullColumnHack, values, CONFLICT_NONE);
        } catch (SQLException e) {
            Log.e(TAG, "Error inserting " + values, e);
            return -1;
        }
    }

方法二:

SQLiteDatabase  db = helper.getWritableDatabase();  
db.execSQL("insert into person(name,phone,amount) values(?,?,?) ",  
        new Object[]{person.getName(),person.getPhone(),person.getAmount()});  
Cursor cursor = db.rawQuery("select last_insert_rowid() from person",null);          
int strid;    
if(cursor.moveToFirst())  
   strid = cursor.getInt(0);  
Log.i("testAuto", strid+""); 

 

参考:

https://blog.csdn.net/Mr_Seng/article/details/53693258

https://blog.csdn.net/fangjjj/article/details/34099099

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值