java - 房间数据库中的列表数组为空

标签 java android-sqlite android-room

我正在使用 Room 持久性库从数据库获取数据,但它返回一个空数组 (nightclub_list)。

只需添加该片段为谷歌地图实现OnMapReadyCallback,并且getMapAsync位于onCreateView中。然而,房间数据库位于“onViewCreated”中。

我在这方面完全是初学者。

实体

@Entity(tableName = "nightclub")
public class NightClub {

    // define variables including a non-null primary key (name)
    @PrimaryKey
    @NonNull
    @ColumnInfo(name = "name")
    public String name;

    @ColumnInfo(name = "latitude")
    public String latitude;

    @ColumnInfo(name = "longitude")
    public String longitude;

    @ColumnInfo(name = "price")
    public String price;

    @ColumnInfo(name = "timeopen")
    public String timeopen;

    @ColumnInfo(name = "link")
    public String link;

    @ColumnInfo(name = "image_link")
    public String image_link;


    // definition of method getName
    public String getName() {
        return name;
    }

    // definition of void function to setName
    public void setName(String club_name) { this.name = club_name; }


    // definition of getLatitude method
    public String getLatitude() {
        return latitude;
    }

    // definition of setLatitude method
    public void setLatitude(String club_latitude ) { this.latitude = club_latitude; }

    // definition of getLongitude method
    public String getLongitude() {
        return longitude;
    }

    // definition of setLongitude method
    public void setLongitude(String club_longitude) {
        this.longitude = club_longitude;
    }

    // definition of getPrice method
    public String getPrice() {
        return price;
    }

    // definition of setPrice method
    public void setPrice(String club_price) {
        this.price = club_price;
    }

    // definition of getTimeOpen method
    public String getTimeOpen() {
        return timeopen;
    }

    // definition of setTimeOpen method
    public void setTimeOpen(String club_timeopen) {
        this.timeopen = club_timeopen;
    }

    // definition of getLink method
    public String getLink() { return link; }

    // definition of setLink method
    public void setLink(String club_link) { this.link = club_link; }

    // definition of getImage method
    public String getImageLink() {
        return image_link;
    }

    // definition of setImage method
    public void setImageLink(String club_imagelink) {
        this.image_link = club_imagelink;
    }

}

DAO

@Dao
public interface NightClubDAO {

    // This interface defines the operations that can be performed in the application from
    // the database
    @Insert
    public void insert(NightClub nightClub);

    @Update
    public void update(NightClub nightClub);

    @Delete
    public void delete(NightClub nightClub);

    @Query("SELECT * FROM nightclub")
    List<NightClub> getAllNightClubs();


}

应用数据库

@Database(entities = {NightClub.class}, version = 3, exportSchema = false)

// this abstract class serves as a connector between the database and the application
public abstract class AppDatabase extends RoomDatabase {

    public abstract NightClubDAO getNightClubDAO();

    // database migration when schema (NightClub) java is altered
    static final Migration MIGRATION_2_3 = new Migration(2, 3) {

        @Override

        public void migrate(SupportSQLiteDatabase database) {

        }

    };


}

map 片段

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        //============ All about the database
        // get database path

        // get context outside else it returns null


        Context context = getContext();
        final File dbFile = context.getDatabasePath(db_name);

//         Log.d(TAG, "here are they: " + dbFile);


        // check if database file exists
        if (!dbFile.exists()) {
            try {
                copyDatabaseFile(dbFile.getAbsolutePath());
                Log.d(TAG, "Reading file database successful");
            } catch (IOException e) {
//                Log.d(TAG, "Reading file database error: " + e);
                e.printStackTrace();
            }
        }


        AppDatabase database =
                Room.databaseBuilder(context, AppDatabase.class, db_name)
                        .allowMainThreadQueries()
                        .addMigrations(MIGRATION_2_3)
                        .build();


        nightclubdao = database.getNightClubDAO(); // from AppDatabase.java

        Log.d(TAG, "here are they: " + database.getNightClubDAO());


        nightclub_list = nightclubdao.getAllNightClubs(); // get all night clubs as a list


        Log.d(TAG, "here are they: " + nightclub_list);


    }

谢谢!

最佳答案

经过多次尝试,问题出在数据库上。多次架构更改和不一致的迁移。我必须完全重写数据库并用数据填充它。

关于java - 房间数据库中的列表数组为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59796192/

相关文章:

java - Vaadin + Spring Boot 中的自定义 servlet

android - 在 Fragment 中打开 Android Sqlite 数据库

Android SQLite 查询 - 获取最新的 10 条记录

android - 在测试 Room AutoMigrations 时,我应该将什么传递给 runMigrationsAndValidate?

java - Java android中Room的CRUD,如何通过viewmodel将Repository中Dao和AsyncTask的删除行数返回给Catalog Activity?

java - 在jsp指令中

java - Java Swing 的 UTF-8 支持问题?

java - 在 dalvik.system.NativeStart.main( native 方法)处崩溃

android - InvalidObjectException ("Remote key and the prevKey should not be null") 创建 RemoteMediator android

安卓工作室。 "Database Inspector is loading"