gpt4 book ai didi

android - 房间 : Database not created

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:47 24 4
gpt4 key购买 nike

我尝试使用来自谷歌架构的房间库。我基于来自谷歌的 BasicSample 编写了一些代码,但没有创建数据库(对于我的代码)。 Logcat 不包含错误和异常。请帮我找出我的错误:

//App.java
//...
public class App extends Application {
private AppExecutors mAppExecutors;
private static final String TAG = "App";

@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate: enter");
mAppExecutors = new AppExecutors();
getDatabase();
}

public AppDatabase getDatabase() {
return AppDatabase.getInstance(this, mAppExecutors);
}
}

我的 AppDatabase 类看起来:

//AppDatabase.java
//...
@Database(entities = {Camera.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static final String DATABASE_NAME = "mydatabase";
private static AppDatabase sInstance;
private AppExecutors mExecutors;
public abstract CameraDao cameraModel();

public static AppDatabase getInstance(final Context context, final AppExecutors executors) {
if (sInstance == null) {
synchronized (AppDatabase.class) {
if (sInstance == null) {
sInstance = buildDatabase(context.getApplicationContext(), executors);
}
}
}
return sInstance;
}
private static final String TAG = "AppDatabase";
private static AppDatabase buildDatabase(final Context context, final AppExecutors executors) {
Log.i(TAG, "buildDatabase: enter");
AppDatabase database = Room.databaseBuilder(context, AppDatabase.class, DATABASE_NAME)
.addCallback(new Callback() {

@Override
public void onCreate(@NonNull SupportSQLiteDatabase db) {
Log.i(TAG, "onCreate: ");
super.onCreate(db);

executors.diskIO().execute(() -> getInstance(context, executors).initDb());
}
})
.build();
database.mExecutors = executors;
Log.i(TAG, "buildDatabase: exit");
return database;
}

private void initDb() {
cameraModel().insert(new Camera(1, "Canon", "EOS 5d Mark III", 1024, 768, 24, 22, 0.0, true));
}
}

道接口(interface):

//CameraDao.java
@Dao
public interface CameraDao {
@Insert
void insert(Camera camera);
}

和实体类:

// Camera.java
@Entity
public class Camera {
@PrimaryKey(autoGenerate = true)
public long id;
public String list_name;
public String camera_name;
public int max_resolution_width;
public int max_resolution_height;
public int max_sensor_width;
public int max_sensor_height;
public double coc;
public boolean is_auto_coc;

public Camera(long id, String list_name, String camera_name, int max_resolution_width, int max_resolution_height, int max_sensor_width, int max_sensor_height, double coc, boolean is_auto_coc) {
this.id = id;
this.list_name = list_name;
this.camera_name = camera_name;
this.max_resolution_width = max_resolution_width;
this.max_resolution_height = max_resolution_height;
this.max_sensor_width = max_sensor_width;
this.max_sensor_height = max_sensor_height;
this.coc = coc;
this.is_auto_coc = is_auto_coc;
}

@Ignore
public Camera(String list_name, String camera_name, int max_resolution_width, int max_resolution_height, int max_sensor_width, int max_sensor_height, double coc, boolean is_auto_coc) {
this.list_name = list_name;
this.camera_name = camera_name;
this.max_resolution_width = max_resolution_width;
this.max_resolution_height = max_resolution_height;
this.max_sensor_width = max_sensor_width;
this.max_sensor_height = max_sensor_height;
this.coc = coc;
this.is_auto_coc = is_auto_coc;
}
}

因此,无法使用此代码创建数据库。未调用创建数据库的回调。顺便说一句,如果我有自动增量字段,是否可以插入自定义值(针对特殊情况)。

我的这段代码的 logcat:

12-04 00:54:47.536 9150-9150/ru.neverdark.photonotes I/App: onCreate: enter

12-04 00:54:47.537 9150-9150/ru.neverdark.photonotes I/AppDatabase: buildDatabase: enter

12-04 00:54:47.541 9150-9150/ru.neverdark.photonotes I/AppDatabase: buildDatabase: exit

最佳答案

在幕后,默认情况下,Room 使用 SQLiteOpenHelper,就像您可以直接使用它一样。

当您创建 SQLiteOpenHelper 实例时,

SQLiteOpenHelper 不会创建数据库。一旦您调用 getReadableDatabase()getWriteableDatabase(),它就会这样做。

从 Room 的角度来看,这意味着直到您执行一些具体的操作,例如调用访问数据库的 @Dao 方法,您的数据库才会被创建。

关于android - 房间 : Database not created,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47619718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com