【问题标题】:Room prepopulated database error: The columns returned by the query does not have the fields [rowid]房间预填充数据库错误:查询返回的列没有字段 [rowid]
【发布时间】:2021-02-09 07:04:03
【问题描述】:

请多多包涵,我对 Android 开发很陌生。我的目标是使用 Room 从 assets 文件夹加载一个带有预填充数据库的应用程序。

问题

经过数小时的搜索,包括所有其他具有相同错误的问题,我无法在 Dao 文件中修复这些错误:

  1. 此错误出现在 methodA():查询返回的列在 com.hfad.appname.ClassName 中没有字段 [rowid,integerB],即使它们被注释为非空或原始。查询返回的列:[textA,integerA]

  2. 此错误出现在 methodB():查询返回的列在 com.hfad.appname.ClassName 中没有字段 [rowid],即使它们被注释为非空或原始。查询返回的列:[textA,textB,integerA,integerB]

示例程序

我通过将 .csv 文件(如下)导入 DB Browser for SQLite 创建了一个 sample.db 文件。

rowid textA textB integerA integerB
Yes No 0 0

DB Browser 中的列是它们各自的 INTEGER 和 TEXT 类型。 (如果 rowid 没有上述值或在其中放置数字都没有关系,错误仍然存​​在。)我将 sample.db 粘贴到 Android Studio 中新创建的资产文件夹中。

Entity ClassName.java 文件:

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Fts4;
import androidx.room.PrimaryKey;

@Fts4
@Entity(tableName = "sample")
public class ClassName {
    @PrimaryKey(autoGenerate = true)
    private int rowid;
    private String textA;
    private String textB;
    private int integerA;
    private int integerB;

    public ClassName(String textA, String textB, int integerA, int integerB){
        this.textA = textA;
        this.textB = textB;
        this.integerA = integerA;
        this.integerB = integerB;
    }
    public int getRowid(){
        return this.rowid;
    }
    public String getTextA(){
        return this.textA;
    }
    public String getTextB(){
        return this.textB;
    }
    public String getIntegerA(){
        return this.integerA;
    }
    public String getIntegerB(){
        return this.integerB;
    }

    public void setRowid(int rowid){
        this.rowid=rowid;
    }
}

Dao ClassNameDao.java 文件:

import androidx.room.Dao;
import androidx.room.Query;
import java.util.List;

@Dao
public interface ClassNameDao {

    @Query("SELECT `textA`, integerA FROM sample")
    public List<ClassName> methodA();

    @Query("SELECT * FROM sample WHERE integerA > :number")
    public ClassName[] methodB(int number);
}

数据库 ClassNameDatabase.java 文件:

import androidx.room.Database;
import androidx.room.RoomDatabase;

@Database(entities={ClassName.class}, version=1)
public abstract class ClassNameDatabase extends RoomDatabase {
    public abstract ClassNameDao getClassNameDao();
}

最后是 MainActivity:

import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Room.databaseBuilder(getApplicationContext(), ClassNameDatabase.class, "sample.db")
                .createFromAsset("sample.db")
                .build();
    }
}

非常感谢任何帮助,谢谢。

【问题讨论】:

    标签: java android sqlite android-room


    【解决方案1】:

    您的 ClassName 定义了 5 列,但查询 methodA() 仅选择 2。

    您可以在查询中选择 *,或者如果您实际上只需要这 2 列,您应该创建一个只定义您要使用的 2 列的新类,并让 methodA() 返回它而不是 @ 987654326@.

    https://developer.android.com/training/data-storage/room/accessing-data#return-subset

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      • 1970-01-01
      相关资源
      最近更新 更多