Reputation: 314
Please bear with me, I am very new to Android development. My goal is to load an app with a prepopulated database from the assets folder using Room.
The problem
After many hours and searches, including all the other questions with this same error, I unable to fix these errors within the Dao file:
This error is at methodA(): The columns returned by the query does not have the fields [rowid,integerB] in com.hfad.appname.ClassName even though they are annotated as non-null or primitive. Columns returned by the query: [textA,integerA]
This error is at methodB(): The columns returned by the query does not have the fields [rowid] in com.hfad.appname.ClassName even though they are annotated as non-null or primitive. Columns returned by the query: [textA,textB,integerA,integerB]
The sample program
I created a sample.db file by importing a .csv file (below) into DB Browser for SQLite.
rowid | textA | textB | integerA | integerB |
---|---|---|---|---|
Yes | No | 0 | 0 |
The columns within DB Browser are their respective INTEGER and TEXT types. (It doesn't matter if the rowid has no values as above or numbers placed within it, the errors remain.) I pasted sample.db into a newly created assets folder in Android Studio.
The Entity ClassName.java file:
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;
}
}
The Dao ClassNameDao.java file:
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);
}
The Database ClassNameDatabase.java file:
import androidx.room.Database;
import androidx.room.RoomDatabase;
@Database(entities={ClassName.class}, version=1)
public abstract class ClassNameDatabase extends RoomDatabase {
public abstract ClassNameDao getClassNameDao();
}
Finally, the 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();
}
}
Any help is much appreciated, thank you.
Upvotes: 0
Views: 171
Reputation: 950
Your ClassName
defines 5 columns, but the query methodA()
is only selecting 2.
You can either select *
in the query, or if you actually only need those 2 columns you should create a new class that only defines the 2 columns you want to use and get methodA()
to return that instead of ClassName
.
See https://developer.android.com/training/data-storage/room/accessing-data#return-subset
Upvotes: 1