标签: android-room

Android Room编译时警告外​​键中的列不是索引的一部分.这是什么意思?

我正在使用最近在Google I/O上宣布的Android架构组件中的Android的Room Persistence Library.事情似乎有效,但我收到以下错误:

警告:tagId列引用外键但它不是索引的一部分.每当修改父表时,这可能会触发全表扫描,因此强烈建议您创建一个涵盖此列的索引.

我的数据库有3个表:Note,Tag,和JoinNotesTags.对标记的注释是多对多关系,因此JoinNotesTags表用于处理映射.表格很简单:

  • Note.id并且Tag.id都是主键
  • JoinNotesTags.noteId 引用 Note.id
  • JoinNotesTags.tagId 引用 Tag.id

外键约束在JoinNotesTags表上定义.作为参考,这是表的CREATE TABLE声明JoinNotesTags:

"CREATE TABLE IF NOT EXISTS `JoinNotesTags` (
    `id` INTEGER PRIMARY KEY AUTOINCREMENT, 
    `noteId` INTEGER, 
    `tagId` INTEGER, 
    FOREIGN KEY(`noteId`) REFERENCES `Note`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE , 
    FOREIGN KEY(`tagId`) REFERENCES `Tag`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION 
)"
Run Code Online (Sandbox Code Playgroud)

这是@Entity该类的相应注释:

@Entity(
        indices = arrayOf(Index(value = *arrayOf("noteId", …
Run Code Online (Sandbox Code Playgroud)

sqlite android android-sqlite android-room

31
推荐指数
3
解决办法
2万
查看次数

Android room persistent library - TypeConverter错误错误:无法弄清楚如何将字段保存到数据库"

由于错误,我无法在房间内创建typeConverter.我似乎每个文档都遵循一切.我想将列表转换为json字符串.让我们来看看我的实体:

      @Entity(tableName = TABLE_NAME)
public class CountryModel {

    public static final String TABLE_NAME = "Countries";

    @PrimaryKey
    private int idCountry;
/* I WANT TO CONVERT THIS LIST TO A JSON STRING */
    private List<CountryLang> countryLang = null;

    public int getIdCountry() {
        return idCountry;
    }

    public void setIdCountry(int idCountry) {
        this.idCountry = idCountry;
    }

    public String getIsoCode() {
        return isoCode;
    }

    public void setIsoCode(String isoCode) {
        this.isoCode = isoCode;
    }

    public List<CountryLang> getCountryLang() {
        return countryLang;
    }

    public void setCountryLang(List<CountryLang> countryLang) { …
Run Code Online (Sandbox Code Playgroud)

android android-room

30
推荐指数
7
解决办法
4万
查看次数

房间"不确定如何将光标转换为此方法的返回类型":哪种方法?

Error:Not sure how to convert a Cursor to this method's return type
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
Run Code Online (Sandbox Code Playgroud)

使用Room我收到此错误,我想找出导致它的方法.

我有多个DAOs,总共有大约60个方法,并且在添加方法之后弹出了这个错误(从另一个完美工作的复制和粘贴,只是将字段更改为设置).

我可以发布整个类的DAOs,但我想知道哪种方法失败了.我试着用Run with --stacktrace,Run with --info--debug option,但这些都不显示出任何有价值的信息.

我添加的方法是一个@Query UPDATEInt返回类型,如在建议的文档

UPDATE或DELETE查询可以返回void或int.如果是int,则该值是受此查询影响的行数.

编辑:我想补充一点,我尝试删除该方法,使DAO回到工作状态,但它仍然给我这个错误.

EDIT2:添加gradle控制台输出,因为在评论中不可读:

error: Not sure how to convert a Cursor to this method's return type
error: Not sure how to convert a Cursor to …
Run Code Online (Sandbox Code Playgroud)

android dao kapt android-room android-architecture-components

30
推荐指数
8
解决办法
2万
查看次数

Android Room:查询中的每个绑定变量都必须具有匹配的方法

我正在使用带有kotlin的android持久性库房间.

Dao看起来像这样

@Dao
interface CountryDao {
    @Query("SELECT * FROM countries")
    fun loadAllCountried() : LiveData<List<CountryEntity>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(products: List<CountryEntity>)

    @Query("SELECT * FROM countries WHERE id = :countryId")
    fun loadCountry(countryId: Int): LiveData<CountryEntity>

    @Query("SELECT * FROM countries WHERE id = :countryId")
    fun loadCountrySync(countryId: Int): CountryEntity

}
Run Code Online (Sandbox Code Playgroud)

这对我来说似乎很好,但我收到了这个错误

Error: Each bind variable in the query must have a matching method parameter. Cannot find method parameters for :countryId.

我可以看到参数被命名为countryId,那么可能是什么问题?

仅供参考:这是CountryDao_Impl.java中的生成代码

@Override
public CountryEntity loadCountrySync(int arg0) {
  final String _sql = "SELECT …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-room

29
推荐指数
4
解决办法
6152
查看次数

如何在Android中使用MVVM时在Room中制作复合键

我刚刚在房间里找到@PrimaryKey注释.所以,如果我想制作复合键,那么如何才能在MVVM中实现呢?

android mvvm android-room

29
推荐指数
3
解决办法
1万
查看次数

Android架构组件:使用枚举

是否可以使用新的Android架构组件和房间持久性库将Enum类型用作Entity类中的嵌入字段?

我的实体(带有嵌入式枚举):

@Entity(tableName = "tasks")
public class Task extends SyncEntity {

    @PrimaryKey(autoGenerate = true)
    String taskId;

    String title;

    /** Status of the given task.
     * Enumerated Values: 0 (Active), 1 (Inactive), 2 (Completed)
     */
    @Embedded
    Status status;

    @TypeConverters(DateConverter.class)
    Date startDate;

    @TypeConverters(StatusConverter.class)
    public enum Status {
        ACTIVE(0),
        INACTIVE(1),
        COMPLETED(2);

        private int code;

        Status(int code) {
            this.code = code;
        }

        public int getCode() {
            return code;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的TypeConverter:

public class StatusConverter {

    @TypeConverter
    public static Task.Status toStatus(int status) {
        if …
Run Code Online (Sandbox Code Playgroud)

android android-room android-architecture-components

28
推荐指数
2
解决办法
2万
查看次数

Android room persistent library - 如何插入具有List对象字段的类

Android房间持久化库中如何将整个Model对象插入到本身具有另一个列表的表中.

让我告诉你我的意思:

@Entity(tableName = TABLE_NAME)
public class CountryModel {

    public static final String TABLE_NAME = "Countries";

    @PrimaryKey
    private int idCountry;

    private List<CountryLang> countryLang = null;

    public int getIdCountry() {
        return idCountry;
    }

    public void setIdCountry(int idCountry) {
        this.idCountry = idCountry;
    }

    public String getIsoCode() {
        return isoCode;
    }

    public void setIsoCode(String isoCode) {
        this.isoCode = isoCode;
    }

    /** 
        here i am providing a list of coutry information how to insert 
        this into db along with CountryModel at same …
Run Code Online (Sandbox Code Playgroud)

android android-room

28
推荐指数
5
解决办法
4万
查看次数

Android Room数据库DAO调试日志

给出像这样的房间数据库DAO:

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Query;

import java.util.Date;
import java.util.List;

@Dao
public interface MyDao {

    @Query("SELECT * FROM MyTable")
    List<MyItem> all();

    @Query("SELECT * FROM MyTable WHERE date = :date AND language = :language")
    MyItem byDate(Date date, String language);


}
Run Code Online (Sandbox Code Playgroud)

有没有办法添加一个Logger或类似的东西,MyDao以便我可以看到正在执行哪些语句.这在开发过程中非常有用,因为我可以立即检查函数是否正确转换为预期的SQL语句.

logging android android-room

28
推荐指数
5
解决办法
1万
查看次数

在Kotlin中使用Room的@ForeignKey作为@Entity参数

我遇到了一个使用类定义注释的Room 教程@PrimaryKey:

@Entity(foreignKeys = @ForeignKey(entity = User.class,
                              parentColumns = "id",
                              childColumns = "userId",
                              onDelete = CASCADE))
public class Repo {
    ...
}
Run Code Online (Sandbox Code Playgroud)

现在,我有以下想要使用主键的数据类:

@Parcel(Parcel.Serialization.BEAN) 
data class Foo @ParcelConstructor constructor(var stringOne: String,
                                              var stringTwo: String,
                                              var stringThree: String): BaseFoo() {

    ...
}
Run Code Online (Sandbox Code Playgroud)

所以,我刚刚@Entity(tableName = "Foo", foreignKeys = @ForeignKey(entity = Bar::class, parentColumns = "someCol", childColumns = "someOtherCol", onDelete = CASCADE))在顶部添加了片段,但我无法编译:

注释不能用作注释参数.

我想知道:为什么(我认为是)使用Java而不是在Kotlin中使用相同的概念?还有,有办法解决这个问题吗?

欢迎所有输入.

java android kotlin java-annotations android-room

27
推荐指数
1
解决办法
5993
查看次数

如何在 Room @Query 中使用参数字段?

我有一个User带有字段的类id,所以我想用 Room 运行以下查询:

@Query("SELECT * FROM ticket where user_id = :user.id")
LiveData<Ticket> loadFromUser(User user);
Run Code Online (Sandbox Code Playgroud)

但是我在 Android Studio on 上收到错误标记,user.id并且我在网上找到的所有示例仅使用该@Query方法的直接参数,通常是 aString或 an int

是否可以在 Room 中使用对象的字段@Query?如果是肯定的,那么引用它的正确方法是什么。

android android-room

27
推荐指数
3
解决办法
5913
查看次数