关于 Android SQLite 中的“_id”字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3192064/
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
About "_id" field in Android SQLite
提问by Kooper
Is the field "_id" necessary in Android SQLite?
Android SQLite 中是否需要字段“_id”?
回答by Al Sutton
_id is useful when you are using the enhanced Adapters which make use of a Cursor (e.g. ResourceCursorAdapter). It's used by these adapters to provide an ID which can be used to refer to the specific row in the table which relates the the item in whatever the adapter is being used for (e.g. a row in a ListView).
_id 在您使用使用 Cursor(例如 ResourceCursorAdapter)的增强型适配器时很有用。这些适配器使用它来提供一个 ID,该 ID 可用于引用表中的特定行,该行与适配器正在使用的任何项目相关联(例如,ListView 中的一行)。
It's not necessary if you're not going to be using classes which need an _id column in a cursor, and you can also use "as _id" to make another column appear as though it's called _id in your cursor.
如果您不打算在游标中使用需要 _id 列的类,则没有必要,您还可以使用“as _id”使另一列看起来好像在您的游标中称为 _id 。
回答by Phil Lello
回答by Aurora
Technically no the field _id
is not required, however if you are making use of the CursorAdapter
class (which you probably are, especially if you are working with the Notepad example) then yes
从技术上讲,该字段_id
不是必需的,但是如果您正在使用CursorAdapter
该类(您可能是,特别是如果您正在使用记事本示例),那么是
"The Cursor must include a column named "_id" or this class will not work"
“光标必须包含名为“_id”的列,否则此类将不起作用”
as explained in the documentation here. Unfortunately the code examples do not make this very clear.
如文档中说明这里。不幸的是,代码示例并没有很清楚地说明这一点。
回答by Brad Hein
It's quite convenient in many cases to have an id field. I prefer mine to be auto-incrementing (as shown below). I'm always finding new uses for the id field :)
在很多情况下,有一个 id 字段是很方便的。我更喜欢我的自动递增(如下所示)。我总是在寻找 id 字段的新用途:)
When it comes time to attach the data to an adapter, I like to use a table name alias to query the id field as _id
. Example: SELECT id _id, msg from message order by id
. That way the adapter sees a field called _id
and everybody's happy.
当需要将数据附加到适配器时,我喜欢使用表名别名来查询 id 字段为_id
. 例子:SELECT id _id, msg from message order by id
。这样适配器就会看到一个被调用的字段_id
,每个人都很高兴。
Here's a sample of how I define my tables:
这是我如何定义表的示例:
CREATE TABLE message (_id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER, tripID TEXT, msg TEXT);
回答by kenju
From the official docs...
从官方文档...
The Cursor must include a column named "_id" or this class will not work. Additionally, using MergeCursor with this class will not work if the merged Cursors have overlapping values in their "_id" columns.
Cursor 必须包含名为“_id”的列,否则此类将不起作用。此外,如果合并的游标在其“_id”列中具有重叠值,则将 MergeCursor 与此类一起使用将不起作用。
And the Cursor
is:
这Cursor
是:
This interface provides random read-write access to the result set returned by a database query.
此接口提供对数据库查询返回的结果集的随机读写访问。
In other words, you need _id
for Android SQLite ( which usually uses Cursor )
换句话说,您需要_id
Android SQLite(通常使用 Cursor )
回答by Jock Nines
If you define your _id column as an autoincrementing integer it is actually an alias for the ROWID column that SQLite provides by default (https://www.sqlite.org/lang_createtable.html#rowid).
如果您将 _id 列定义为自动递增的整数,它实际上是 SQLite 默认提供的 ROWID 列的别名(https://www.sqlite.org/lang_createtable.html#rowid)。
Your create statement needs take the form...
您的 create 语句需要采用以下形式...
CREATE TABLE t(_id INTEGER PRIMARY KEY ASC, y, z);
To prove this works...
为了证明这是有效的...
UPDATE t SET _id=22 WHERE _id=11;
then
然后
SELECT ROWID, _id FROM t;
and you'll find both _id
and ROWID
have the same value.
你会发现两者_id
并ROWID
具有相同的值。
Note, that if you use DESC in the CREATE a new column is created and ROWID
is not aliased.
请注意,如果您在 CREATE 中使用 DESC,则会创建一个新列并且ROWID
没有别名。
回答by Geucimar
Surely not. Its a convenience field that some widgets like ListView uses to populate data. See this good article: http://www.casarini.org/blog/2009/android-contentprovider-on-sqlite-tables-without-the-_id-column/
当然不是。它是一些像 ListView 这样的小部件用来填充数据的便利字段。看到这篇好文章:http: //www.casarini.org/blog/2009/android-contentprovider-on-sqlite-tables-without-the-_id-column/
回答by Yaojin
Of course if you are creating your own UI widget and your own adapter, you don't have to name your primary key as "_id". It can be any name you want. But you would be responsible for managing your collections of UI widgets and binding them to the right row in your database. "_id" is only useful for ListView as Brad has pointed out.
当然,如果您要创建自己的 UI 小部件和自己的适配器,则不必将主键命名为“_id”。它可以是您想要的任何名称。但是您将负责管理您的 UI 小部件集合并将它们绑定到数据库中的正确行。正如布拉德指出的那样,“_id”仅对 ListView 有用。
回答by Shubham Kumar
The _id field is indeed necessary in sqlite, it will help you to select a particular data from sqlite.
_id 字段在 sqlite 中确实是必需的,它将帮助您从 sqlite 中选择特定的数据。
SELECT name from table_name where _id = ?
And if your are creating a recyclerview/ listview and you want a detailed activity for that list item you indeed need an id for this to fetch data of that item.
如果您正在创建一个 recyclerview/listview 并且您想要该列表项的详细活动,您确实需要一个 id 来获取该项目的数据。
if you are creating a class for constants there is a BaseColumn interface in android, which provide _ID field to that constant class.
如果您正在为常量创建一个类,那么 android 中有一个 BaseColumn 接口,它为该常量类提供 _ID 字段。
//from android documentation..
public static class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "entry";
public static final String COLUMN_NAME_TITLE = "title";
public static final String COLUMN_NAME_SUBTITLE = "subtitle";
}