私はGoogleの建築から部屋のライブラリを試してみます。私は、Googleからの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));
}
}
ダオ・インターフェース:
//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;
}
}
、とこのコードデータベースは作成できませんでした。 create dbのコールバックが呼び出されません。 Btw、私はautoincrementフィールドを持っている場合は、(特別な条件のために)カスタム値を挿入することが可能です。
このコードのための私のlogcat:
12月4日00:54:47.536 9150から9150/ru.neverdark.photonotes I /アプリケーションは:のonCreate:
12月4日午後12時54分を入力します。 :47.537 9150から9150/ru.neverdark.photonotes I/AppDatabase:buildDatabaseは、次のように入力します。
12月4日00:54:47.541 9150から9150/ru.neverdark.photonotes I/AppDatabase:buildDatabase:終了
"データベースが作成されていません" - これを使用するコードはどこにありますか? 'AppDatabase.getInstance()'はデータベースを作成せず、 'SQLiteOpenHelper'のインスタンスを作成する以上にデータベースを作成します。あなたが最初にデータベースを作成することを読んだり書いたりしてみることができます。 – CommonsWare
@CommonsWare素晴らしい!問題が解決しました – Artem