请问下SQLite怎么获得id的最大值?谢谢各位

ljabc2010 2012-05-06 10:10:20
能用select max(id) from table吗?execSQL好像不能执行select语句,并且这个函数没有返回值,我想取得id的最大值存到一个变量里面,怎么弄啊,谢谢
...全文
4162 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
gangbener 2012-11-22
  • 打赏
  • 举报
回复
引用 13 楼 hanxiaoyu1988 的回复:
select max(_id) AS maxId from tableName String strSql = "select max(_id) AS maxId from tableName"; Cursor mCursor = db.rawQuery(strSql, null); int maxId = cursor.getInt(cursor.get……
嘿,你好,我使用上述代码的时候,出现了: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 异常,解决方法如下: Cursor mCursor = db.rawQuery(strSql, null); mCursor.moveToNext(); int maxId = mCursor .getInt(mCursor.getColumnIndex(maxId)); 你没有遇到过该异常吗? 另:如有错误,欢迎指出。 Thanks.
倾枫 2012-05-08
  • 打赏
  • 举报
回复
楼上已经给出了正确答案
max(_id)
booooo­ 2012-05-07
  • 打赏
  • 举报
回复
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("table_name", null, null, null, null, null, "ORDER BY ID DESC");
if(cursor.moveToNext()
{
int id = cursor.getInt(cursor.getColumnIndex("_id"));
// 这个id就是最大值
}
booooo­ 2012-05-07
  • 打赏
  • 举报
回复
select id from table order by id desc;

得到结果的第一个就是ID的最大值;
msgsnd 2012-05-07
  • 打赏
  • 举报
回复
支持rawquery[Quote=引用 4 楼 的回复:]

查询语句不是用execSQL,而是用另一个rawXXX的,返回的Cursor的变量,你可以谷歌下。
哎。。。,eclipse自动提示功能让程序员变弱智了
[/Quote]
qdkfriend 2012-05-07
  • 打赏
  • 举报
回复
sqllite是支持max函数的
检查检查你的表数据和字段
endual 2012-05-07
  • 打赏
  • 举报
回复
查询语句不是用execSQL,而是用另一个rawXXX的,返回的Cursor的变量,你可以谷歌下。
哎。。。,eclipse自动提示功能让程序员变弱智了
啊腻不腻 2012-05-07
  • 打赏
  • 举报
回复
select max(_id) AS maxId from tableName
String strSql = "select max(_id) AS maxId from tableName";

Cursor mCursor = db.rawQuery(strSql, null);

int maxId = cursor.getInt(cursor.getColumnIndex(maxId));
a-white-lie 2012-05-07
  • 打赏
  • 举报
回复
查询 表中的count总数 不就行了吗?
ljabc2010 2012-05-07
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

引用 9 楼 的回复:

引用 4 楼 的回复:

查询语句不是用execSQL,而是用另一个rawXXX的,返回的Cursor的变量,你可以谷歌下。
哎。。。,eclipse自动提示功能让程序员变弱智了

那怎么从Cursor中得到我要的这个ID值呢?


Cursor c = ....

String id = c.getString("_id", 0);
[/Quote]
这个函数不对啊,public abstract String getString (int columnIndex)
李狗蛋52635 2012-05-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 4 楼 的回复:

查询语句不是用execSQL,而是用另一个rawXXX的,返回的Cursor的变量,你可以谷歌下。
哎。。。,eclipse自动提示功能让程序员变弱智了

那怎么从Cursor中得到我要的这个ID值呢?
[/Quote]

Cursor c = ....

String id = c.getString("_id", 0);
ljabc2010 2012-05-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

查询语句不是用execSQL,而是用另一个rawXXX的,返回的Cursor的变量,你可以谷歌下。
哎。。。,eclipse自动提示功能让程序员变弱智了
[/Quote]
那怎么从Cursor中得到我要的这个ID值呢?
ljabc2010 2012-05-06
  • 打赏
  • 举报
回复
咋没有人帮下忙呢
ljabc2010 2012-05-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

ContentValue cv=new ConetValue();//这个cv里面什么也不放,把这个cv插入到数据库中就会返回_id
[/Quote]
没看懂啊
aBOUNTWINTER 2012-05-06
  • 打赏
  • 举报
回复
ContentValue cv=new ConetValue();//这个cv里面什么也不放,把这个cv插入到数据库中就会返回_id

80,472

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧