获取SQLite上最后一行的ID的最佳方法 [英] Best way to get the ID of the last inserted row on SQLite

查看:1571
本文介绍了获取SQLite上最后一行的ID的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone上,使用FMDB获取SQLite数据库上最后插入行的ID的最佳方法是什么?

On iPhone, what's the best way to get the ID of the last inserted row on an SQLite Database using FMDB ?

是否有更好的方法而不是:

Is there a better way rather than doing :

SELECT MAX(ID)


推荐答案

如果您可以保证 ID 列是自动增量列, MAX(ID)没问题。

If you can guarantee that your ID column is an auto-increment column, MAX(ID) is fine.

但是为了涵盖任何情况,有一个专门的SQLite函数叫做 LAST_INSERT_ROWID()

But to cover any case, there's a specialized SQLite function called LAST_INSERT_ROWID():

SELECT LAST_INSERT_ROWID();

在FMDB中,您使用 -lastInsertRowId 方法(内部运行上面的代码):

In FMDB, you use the -lastInsertRowId method (which internally runs the above):

int lastId = [fmdb lastInsertRowId];

这篇关于获取SQLite上最后一行的ID的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆