通过从另一个 table(房间图书馆)提供字段来从 table 获取字段
Get field from table by giving field from another table (Room Library)
我正在学习如何在 Android 中使用 Room。我有两个表:KeyWordsTable (id, keyword) 和 CategoriesTable (id, imagePath)。我想做这样的事情:
用户输入关键字 -> 检查它属于哪个类别 -> 从 CategoriesTable 中获取 imagePath。
我有 @Dao 界面,我做了类似
的事情
@Query("SElECT image_path AS imagePath FROM CategoriesTable JOIN KeywordsTable ON idKeyWords = idCategories WHERE category_name = :categoryName LIMIT 1")
CategoriesTable findImagePathByKeyWordName(String categoryName);
这是正确的解决方案吗?我会通过提供关键字来获取 imagePath 吗?
谢谢指教:)
CategoriesTable.class
@Entity
public class CategoriesTable {
@NonNull
@PrimaryKey(autoGenerate = true)
private int idCategories;
@ColumnInfo(name = "category_name")
private String categoryName;
@ColumnInfo(name = "image_path")
private String imagePath;
//getters and setters
KeywordsTable.class
@Entity
public class KeywordsTable {
@PrimaryKey(autoGenerate = true)
private int idKeyWords;
@ColumnInfo(name = "keywords_name")
private String keywordsName;
//getters and setters
首先,您必须在关系的 many
端提供外键。此外,您应该在 @Entity
注释中设置每个 table 的名称。
KeywordsTable.java
@Entity(tableName = "keywords",
foreignKeys = @ForeignKey(entity = CategoriesTable.class,
parentColumns = "idCategories",
childColumns = "categoryId",
onDelete = CASCADE))
public class KeywordsTable {
@PrimaryKey(autoGenerate = true)
private int idKeyWords;
@ColumnInfo(name = "keywords_name")
private String keywordsName;
private int categoryId;
//getters and setters
}
CategoriesTable.java
@Entity(tableName = "categories")
public class CategoriesTable {
@PrimaryKey(autoGenerate = true)
private int idCategories;
@ColumnInfo(name = "category_name")
private String categoryName;
@ColumnInfo(name = "image_path")
private String imagePath;
//getters and setters
}
dao 接口中的方法应该如下所示:
@Query("SELECT image_path FROM categories INNER JOIN keywords" +
"ON idCategories = categoryId WHERE keywords_name = :keyword LIMIT 1")
String findImagePathByKeyWordName(String keyword);
我正在学习如何在 Android 中使用 Room。我有两个表:KeyWordsTable (id, keyword) 和 CategoriesTable (id, imagePath)。我想做这样的事情: 用户输入关键字 -> 检查它属于哪个类别 -> 从 CategoriesTable 中获取 imagePath。
我有 @Dao 界面,我做了类似
的事情@Query("SElECT image_path AS imagePath FROM CategoriesTable JOIN KeywordsTable ON idKeyWords = idCategories WHERE category_name = :categoryName LIMIT 1")
CategoriesTable findImagePathByKeyWordName(String categoryName);
这是正确的解决方案吗?我会通过提供关键字来获取 imagePath 吗?
谢谢指教:)
CategoriesTable.class
@Entity
public class CategoriesTable {
@NonNull
@PrimaryKey(autoGenerate = true)
private int idCategories;
@ColumnInfo(name = "category_name")
private String categoryName;
@ColumnInfo(name = "image_path")
private String imagePath;
//getters and setters
KeywordsTable.class
@Entity
public class KeywordsTable {
@PrimaryKey(autoGenerate = true)
private int idKeyWords;
@ColumnInfo(name = "keywords_name")
private String keywordsName;
//getters and setters
首先,您必须在关系的 many
端提供外键。此外,您应该在 @Entity
注释中设置每个 table 的名称。
KeywordsTable.java
@Entity(tableName = "keywords",
foreignKeys = @ForeignKey(entity = CategoriesTable.class,
parentColumns = "idCategories",
childColumns = "categoryId",
onDelete = CASCADE))
public class KeywordsTable {
@PrimaryKey(autoGenerate = true)
private int idKeyWords;
@ColumnInfo(name = "keywords_name")
private String keywordsName;
private int categoryId;
//getters and setters
}
CategoriesTable.java
@Entity(tableName = "categories")
public class CategoriesTable {
@PrimaryKey(autoGenerate = true)
private int idCategories;
@ColumnInfo(name = "category_name")
private String categoryName;
@ColumnInfo(name = "image_path")
private String imagePath;
//getters and setters
}
dao 接口中的方法应该如下所示:
@Query("SELECT image_path FROM categories INNER JOIN keywords" +
"ON idCategories = categoryId WHERE keywords_name = :keyword LIMIT 1")
String findImagePathByKeyWordName(String keyword);