ric*_*hah 5 android android-room android-architecture-components
我指的是 Room 数据库的索引特定列。
下面是一些示例代码写在这里https://developer.android.com/training/data-storage/room/defining-data#column-indexing
示例代码-1:
@Entity(indices = {@Index("name"),
@Index(value = {"last_name", "address"})})
public class User {
@PrimaryKey
public int id;
public String firstName;
public String address;
@ColumnInfo(name = "last_name")
public String lastName;
@Ignore
Bitmap picture;
}
Run Code Online (Sandbox Code Playgroud)
示例代码 2:
@Entity(indices = {@Index(value = {"first_name", "last_name"},
unique = true)})
public class User {
@PrimaryKey
public int id;
@ColumnInfo(name = "first_name")
public String firstName;
@ColumnInfo(name = "last_name")
public String lastName;
@Ignore
Bitmap picture;
}
Run Code Online (Sandbox Code Playgroud)
这在 android 的房间文档中有所描述,我使用了索引来表示列的唯一性,但是上面的代码意味着什么,谁能解释一下?
Q1:索引和@Index有什么用?
Q2:是什么区别@Index("name")和@Index(value = {"last_name", "address"})?
Bil*_*lal 10
回答有点晚了。但希望它会帮助某人。
Q1:indicesand 有@Index什么用?
Indices:可以包含表上的索引列表。而@Index用于定义索引。
例如:
@Entity(indices = {
@Index("FirstIndex"),@Index(value="last_name",unique = true),
@Index("SecondIndex"),@Index(value="first_name", unique = true)
})
Run Code Online (Sandbox Code Playgroud)
如您所见,我在此示例中定义了两个索引,它们由昏迷分隔。这里的“FirstIndex”和“SecondIndex”是索引的名称。如果我没有定义索引的名称(如您在示例中引用的那样),那么 Room 会将其设置为由 ' ' 连接并以“index ${tableName}”为前缀的列列表。因此,如果您有一个名为“Foo”且索引为 {"bar", "baz"} 的表,则生成的索引名称将为“index_Foo_bar_baz”。
Q2:是什么区别@Index("name")和@Index(value = {"last_name", "address"})?
@index("name") //syntax to give name of index.
@index(value=last_name", "address") //Syntax to define, which column(s) need to be indexed
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7304 次 |
| 最近记录: |