【发布时间】:2021-12-17 04:49:02
【问题描述】:
我想尝试用 Java(不在 Kotlin 中)的 android studio 中制作一个带有 Room Library 的表格来容纳 2 列。
这里的问题是我的数据是这样的:
| Year | Dowry |
|---|---|
| 1317 | 0.004 |
| 1318 | 0.004 |
| 1319 | 0.004 |
| 1320 | 0.008 |
| 1400 | 0.255 |
为此,我创建了一个名为 Data 的类,并且我这样评价它:
@Entity(tableName = "tbl_data")
public class Data {
@PrimaryKey(autoGenerate = true)
private long id;
private int year;
private int dowry;
public Data(int year, int dowry) {
this.year = year;
this.dowry = dowry;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getDowry() {
return dowry;
}
public void setDowry(int dowry) {
this.dowry = dowry;
}
}
然后我做了一个这样的数据库:
@Database(version = 1 , exportSchema = false , entities = {Data.class} )
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase appDatabase;
public static AppDatabase getAppDatabase(Context context) {
appDatabase= Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class,
"db_app")
.allowMainThreadQueries()
.build();
return appDatabase;
}
public abstract DataDao getDataDao();
}
如何将数据添加到我的 data.class AKA "tbl_data" 中?
【问题讨论】:
标签: java android mysql android-room