Android 如何从游标中获取行 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2848056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get a row ID from a Cursor
提问by oriharel
How do I get the row ID from a Cursor?
如何从 Cursor 获取行 ID?
回答by Nic Strong
I don't think the Cursor exposes this directly.SQLiteDatabase.insert()
returns the row id of the newly inserted row. Or in Android the convention is that there is a column named "_id"
that contains the primary autoincrement key of the table. So cursor.getLong(cursor.getColumnIndex("_id"))
would retrieve this.
我认为 Cursor 不会直接暴露这一点。SQLiteDatabase.insert()
返回新插入行的行 ID。或者在 Android 中,约定是有一个名为的列"_id"
包含表的主要自动增量键。所以cursor.getLong(cursor.getColumnIndex("_id"))
会检索这个。
回答by Luigi Putanesca
I had this same problem where the column index for the primary key was reported as -1 (meaning it isn't there). The problem was that I forgot to include the _ID column in the initial SELECT clause that created the cursor. Once I made sure it was included, the column was accessible just like any of the others.
我遇到了同样的问题,其中主键的列索引报告为 -1(意味着它不存在)。问题是我忘记在创建游标的初始 SELECT 子句中包含 _ID 列。一旦我确定它被包含在内,该列就可以像其他任何列一样访问。
回答by CP Taylor
Concerning the last sentence of Nic Strong's answer,following command didn't work for me. cursor.getColumnIndex("_id")
was still -1
关于 Nic Strong 回答的最后一句话,以下命令对我不起作用。cursor.getColumnIndex("_id")
仍然是-1
cursor.getLong(cursor.getColumnIndex("_id"))
Maybe there's some other issue in my configuration that's causing the problem?
也许我的配置中还有其他一些问题导致了这个问题?
Personally I've taken to maintaining my own custom unique id column in each table I create; A pain, but it gets around this issue.
我个人已经开始在我创建的每个表中维护我自己的自定义唯一 id 列;一个痛苦,但它解决了这个问题。
回答by Arkadiusz Cie?liński
return sqlite_db.query(table, new String[] { "rowid", "*" }, where, args, null, null, null);
In my case I have "rowid" in DataManager.FIELD_ID and this is SQLite identity column (each table in sqlite has this special kind of column), so I don't need any of my own custom unique id column in tables.
在我的情况下,我在 DataManager.FIELD_ID 中有“rowid”,这是 SQLite 标识列(sqlite 中的每个表都有这种特殊类型的列),所以我不需要表中任何我自己的自定义唯一 id 列。
回答by Ferran Maylinch
Cursor cursor = mySQLiteHelper.getReadableDatabase().query(TABLE_NAME, new String[] { "ROWID", "*" }, where, null, null, null, null);
then
然后
long rowId = cursor.getLong(0);
回答by MikeT
As long as the TABLE is not defined using WITHOUT ROWID
you can get the ROWID(or should that be rowidsee below for case) if you specify it as a column to be retrieved.
只要 TABLE 没有使用定义,如果您将其指定为要检索的列,WITHOUT ROWID
您就可以获得ROWID(或者应该是rowid,请参见下面的案例)。
For example for a table with 3 defined columns (Names, Colour and Age) with no conventional _idcolumn. The following rawQuery works and returns the ROWID:-
例如,对于具有 3 个已定义列(名称、颜色和年龄)且没有常规_id列的表。以下 rawQuery 工作并返回ROWID:-
Cursor csr = db.rawQuery("SELECT Names, Colour, Age, ROWID FROM " + TABLE_NAME,null);
Note! that the column name in the cursor is lowercaseas per :-
笔记!游标中的列名是小写的,如下所示:-
Note! ROWIDin the SELECT SQLis case independent (e.g. RoWiD works).
笔记!SELECT SQL中的ROWID与大小写无关(例如 RoWiD 工作)。
Using
使用
Cursor csr = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
WILL NOTreturn the ROWID(likewise for null for the columns when using the query
method).
不会返回ROWID(对于使用该query
方法时列的 null 也是如此 )。
Using query
(as opposed to rawQuery
) works in the same way, that is you need to specifiy ROWID (note alternatives to ROWID below) as a column to be retrieved e.g. :-
使用query
(而不是rawQuery
) 的工作方式相同,即您需要指定 ROWID(注意下面 ROWID 的替代方案)作为要检索的列,例如:-
Cursor csr = db.query(TABLE_NAME,new String[]{
"OiD",
"Names",
"Colour",
"Age"
},null,null,null,null,null);
Alternatives to ROWID
ROWID 的替代品
Instead of ROWID
, you can also use _ROWID_
or OID
(case independent) as the column name in the query noting that the column name in the resultant cursor is rowidi.e. it is lowercase.
代替ROWID
,您还可以使用_ROWID_
or OID
(大小写无关)作为查询中的列名,注意结果游标中的列名是rowid ,即它是小写的。