我正在编写一个数据库,用于保存Android应用程序中有关单词的信息。我建立了两个实体:Group和VocaNote。在Group和VocaNote之间,我使用一对多关系将VocaNote数组保存在不同的组中。
小组课程
@Entity(indices = {
@Index(value = {"language", "nameGroup"}, unique = true)
}
)
public class Group {
@PrimaryKey(autoGenerate = true)
public long id;
@ColumnInfo(name = "language")
public String language;
@ColumnInfo(name = "name_group")
public String nameGroup;
}
VocaNote类
@Entity(foreignKeys = @ForeignKey(entity = Group.class,
parentColumns = "id",
childColumns = "group_id",
onDelete = CASCADE))
public class VocaNote {
@PrimaryKey(autoGenerate = true)
public long id;
@ColumnInfo(name = "origin_word")
public String originWord;
@ColumnInfo(name = "translation")
public String translation;
@ColumnInfo(name = "studied_word")
public int studied;
@ColumnInfo(name = "group_id")
public int groupId;
}
之后,我为上述实体类添加了单独的DAO
VocaNoteDao
@Dao
public interface VocaNoteDao {
@Query("SELECT * FROM vocanote")
List<VocaNote> getAllVocaNote();
@Query("SELECT * FROM vocanote WHERE id = :id")
VocaNote getByIdVocaNote(long id);
@Insert
void insert(VocaNote vocaNote);
@Update
void update(VocaNote vocaNote);
@Delete
void delete (VocaNote vocaNote);
}
GroupDao
@Dao
public interface GroupDao {
@Insert
void insert(Group group);
@Update
void update(Group group);
@Delete
void delete (Group group);
@Query("SELECT * FROM group")
List<Group> getAllGroup();
@Query("SELECT * FROM group WHERE id = :id")
Group getByIdGroup(long id);
}
这里我遇到了问题。当我尝试从组表进行查询时,而是收到编译错误表或子查询预期,得到组。 请告诉我,我该怎么处理?我有组实体,但我的IDE不想看到/
答案 0 :(得分:1)
使用方括号转义保留关键字
@Query("SELECT * FROM [group] WHERE id = :id")
Group getByIdGroup(long id);
答案 1 :(得分:-1)
您必须检查表名。必须填写表名。甚至不能输入空字符串。