如何一次创建并存储在数据库对象实例中(用户安装应用程序时)
How to create and store in the database object instance ONCE (while user installing the app)
我有一个 android 项目正在进行中,用户将有机会玩一些游戏,例如 tik tak toe、Rock–paper–scissors 等。对于数据库,我使用 Room 持久性,所以我想创建 12 个实例,每个游戏一个。每个游戏都有名称、targetSkill 等 etcGame tic_tac_toe = new Game("tic tac toe", "attention"...) 。我想在用户安装应用程序时创建这 12 个实例,然后将它们存储在数据库中。我不知道该怎么做。我应该在哪里放置实例的代码以便 运行 一次?
这是游戏的模型 (Game class)
@Entity(tableName = "games")
public class Game {
@PrimaryKey(autoGenerate = true)
@NonNull
private int id;
@ColumnInfo(name = "name")
@NonNull
private String name;
@ColumnInfo(name = "targetSkill")
@NonNull
private String targetSkill;
@ColumnInfo(name = "description")
@NonNull
private String description;
@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
private byte[] image;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@NonNull
public String getName() {
return name;
}
public void setName(@NonNull String name) {
this.name = name;
}
@NonNull
public String getTargetSkill() {
return targetSkill;
}
public void setTargetSkill(@NonNull String targetSkill) {
this.targetSkill = targetSkill;
}
@NonNull
public String getDescription() {
return description;
}
public void setDescription(@NonNull String description) {
this.description = description;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public Game(@NonNull String name, @NonNull String targetSkill, @NonNull String description) {
this.name = name;
this.targetSkill = targetSkill;
this.description = description;
}
@Ignore
public Game(@NonNull String name, @NonNull String targetSkill, @NonNull String description, byte[] image) {
this.name = name;
this.targetSkill = targetSkill;
this.description = description;
this.image = image;
}
}
我还有一个 java Class 游戏助手,我在其中创建游戏实例
字符串 stoneDesc = "the screen displays 2 images (rock, paped, scissor) and the user has to pick " +
"the appropriate image. It depends on Lose or Win Mode";
Game Rock =new Game("Rock","skill",stoneDesc,logoImage);
您只需像这样预填充您的数据库:
- 应用数据库:
@Database(entities = {City.class, WeekForecastItem.class,
CurrentForecast.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract CityDao cityDao();
public abstract WeekForecastItemDao weekForecastItemDao();
public abstract CurrentForecastDao currentForecastDao();
public synchronized static AppDatabase getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = buildDatabase(context);
}
return INSTANCE;
}
private static AppDatabase buildDatabase(final Context context) {
return Room.databaseBuilder(context,
AppDatabase.class,
"my-database")
.addCallback(new Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
Executors.newSingleThreadScheduledExecutor().execute(new Runnable() {
@Override
public void run() {
getInstance(context).cityDao().insertAll(City.populateCityData());
}
});
}
})
.build();
}
}`
- 而我们将用作数据模型的 class,在您的情况下它可能是游戏 class(注意,它具有 populateCityData() 方法):
@Entity
public class City {
@PrimaryKey(autoGenerate = true)
public long id;
public String name;
public int woeid;
public City(String name, int woeid) {
this.name = name;
this.woeid = woeid;
}
public static City[] populateCityData() {
return new City[]{
new City("Saint-Petersburg", 2123260),
new City("Moscow", 2122265),
};
}
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public int getWoeid() {
return woeid;
}
}`
它对我有类似任务的项目有效)
您可以通过两种方式在 Room 中执行此操作。
您可以创建一个 migration 添加这些硬编码游戏。这样,当应用程序第一次连接到数据库时,它将 运行 您的插入代码。这种方法的缺点是您不能使用必须 运行 原始 sql 的 DAO。但是你保证每个只有一个实例,因为迁移只会在数据库版本之间 运行。
在您启动时 activity 您可以 运行 一个任务(Coroutine、AsyncTask 等),它会首先检查游戏是否存在,然后将它们插入如果缺席。每个游戏最多需要 2 个查询,但它允许您在不更新数据库版本的情况下动态添加更多游戏。
我自己更喜欢数字 2,它需要更多的处理,但更灵活。
我有一个 android 项目正在进行中,用户将有机会玩一些游戏,例如 tik tak toe、Rock–paper–scissors 等。对于数据库,我使用 Room 持久性,所以我想创建 12 个实例,每个游戏一个。每个游戏都有名称、targetSkill 等 etcGame tic_tac_toe = new Game("tic tac toe", "attention"...) 。我想在用户安装应用程序时创建这 12 个实例,然后将它们存储在数据库中。我不知道该怎么做。我应该在哪里放置实例的代码以便 运行 一次? 这是游戏的模型 (Game class)
@Entity(tableName = "games")
public class Game {
@PrimaryKey(autoGenerate = true)
@NonNull
private int id;
@ColumnInfo(name = "name")
@NonNull
private String name;
@ColumnInfo(name = "targetSkill")
@NonNull
private String targetSkill;
@ColumnInfo(name = "description")
@NonNull
private String description;
@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
private byte[] image;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@NonNull
public String getName() {
return name;
}
public void setName(@NonNull String name) {
this.name = name;
}
@NonNull
public String getTargetSkill() {
return targetSkill;
}
public void setTargetSkill(@NonNull String targetSkill) {
this.targetSkill = targetSkill;
}
@NonNull
public String getDescription() {
return description;
}
public void setDescription(@NonNull String description) {
this.description = description;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public Game(@NonNull String name, @NonNull String targetSkill, @NonNull String description) {
this.name = name;
this.targetSkill = targetSkill;
this.description = description;
}
@Ignore
public Game(@NonNull String name, @NonNull String targetSkill, @NonNull String description, byte[] image) {
this.name = name;
this.targetSkill = targetSkill;
this.description = description;
this.image = image;
}
}
我还有一个 java Class 游戏助手,我在其中创建游戏实例
字符串 stoneDesc = "the screen displays 2 images (rock, paped, scissor) and the user has to pick " + "the appropriate image. It depends on Lose or Win Mode";
Game Rock =new Game("Rock","skill",stoneDesc,logoImage);
您只需像这样预填充您的数据库:
- 应用数据库:
@Database(entities = {City.class, WeekForecastItem.class,
CurrentForecast.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract CityDao cityDao();
public abstract WeekForecastItemDao weekForecastItemDao();
public abstract CurrentForecastDao currentForecastDao();
public synchronized static AppDatabase getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE = buildDatabase(context);
}
return INSTANCE;
}
private static AppDatabase buildDatabase(final Context context) {
return Room.databaseBuilder(context,
AppDatabase.class,
"my-database")
.addCallback(new Callback() {
@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
Executors.newSingleThreadScheduledExecutor().execute(new Runnable() {
@Override
public void run() {
getInstance(context).cityDao().insertAll(City.populateCityData());
}
});
}
})
.build();
}
}`
- 而我们将用作数据模型的 class,在您的情况下它可能是游戏 class(注意,它具有 populateCityData() 方法):
@Entity
public class City {
@PrimaryKey(autoGenerate = true)
public long id;
public String name;
public int woeid;
public City(String name, int woeid) {
this.name = name;
this.woeid = woeid;
}
public static City[] populateCityData() {
return new City[]{
new City("Saint-Petersburg", 2123260),
new City("Moscow", 2122265),
};
}
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public int getWoeid() {
return woeid;
}
}`
它对我有类似任务的项目有效)
您可以通过两种方式在 Room 中执行此操作。
您可以创建一个 migration 添加这些硬编码游戏。这样,当应用程序第一次连接到数据库时,它将 运行 您的插入代码。这种方法的缺点是您不能使用必须 运行 原始 sql 的 DAO。但是你保证每个只有一个实例,因为迁移只会在数据库版本之间 运行。
在您启动时 activity 您可以 运行 一个任务(Coroutine、AsyncTask 等),它会首先检查游戏是否存在,然后将它们插入如果缺席。每个游戏最多需要 2 个查询,但它允许您在不更新数据库版本的情况下动态添加更多游戏。
我自己更喜欢数字 2,它需要更多的处理,但更灵活。