我刚刚为我的应用程序完成了以下教程: https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#4
但是我的问题是,即使我插入了数据,我的数据库仍然是空的。我通过在数据库浏览器中查看来检查它,并且其中没有记录。任何帮助将不胜感激。如果您需要更多信息。让我知道。
Dao对象类:
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertRecord(LocationDetails Details);
我的实体类:
Entity(tableName = "location")
公共类LocationDetails {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@PrimaryKey(autoGenerate = true)
private int id;
private String jsonObjectString;
private double latitude;
private double longitude;
private String locationType;
public String getLocationType() {
return locationType;
}
public void setLocationType(String locationType) {
this.locationType = locationType;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public String getJsonObjectString() {
return jsonObjectString;
}
public void setJsonObjectString(String jsonObjectString) {
this.jsonObjectString = jsonObjectString;
}
public String toString() {
return "Lat: " + latitude + ", Long: " + longitude;
}
}
这是我的数据库控制器类,我的ViewModel用于与数据库交互。
公共类DatabaseController {
private String TAG = getClass().getSimpleName();
private static DatabaseController sInstance;
private LiveData<List<LocationDetails>> mListOfLocations;
private Database mDatabase;
private DatabaseController(Context context) {
mDatabase = Database.getDatabase(context);
}
public static DatabaseController getInstance() {
return sInstance;
}
public static void initInstance(Context context) {
sInstance = new DatabaseController(context);
}
public void addLocationRecord(LocationDetails locationDetails) {
new insertAsyncTask(mDatabase).execute(locationDetails);
Log.d(TAG, "Location Record Added Successfully");
}
private static class insertAsyncTask extends AsyncTask<LocationDetails, Void, Void> {
private Database database;
insertAsyncTask(Database database) {
this.database = database;
}
@Override
protected Void doInBackground(LocationDetails... databases) {
database.mMyDao().insertRecord(databases[0]);
return null;
}
}
已经在每个步骤添加了断点并进行了检查。整个代码运行无任何异常或错误,但仍然没有在我的数据库中添加任何记录。
这里回答的问题是相同的,但是他的解决方案是不同的,因此不能重复。 Room Database Table is still empty after inserting data
我第一次收到这些日志:
2018-10-26 18:18:41.329 27827-27827/genesis.com.getlocation D/DatabaseController: Location Record Added Successfully
2018-10-26 18:18:41.345 27827-27876/genesis.com.getlocation D/Surface: Surface::disconnect(this=0x990a1f00,api=1)
2018-10-26 18:18:41.346 27827-27944/genesis.com.getlocation W/SQLiteConnection: Could not change the database journal as DB was busy. Proceeding...
2018-10-26 18:18:41.347 27827-27876/genesis.com.getlocation D/GraphicBuffer: unregister, handle(0xaaf92040) (w:848 h:1264 s:848 f:0x1 u:0x000b00)
2018-10-26 18:18:41.347 27827-27876/genesis.com.getlocation D/GraphicBuffer: unregister, handle(0xaaf92400) (w:848 h:1264 s:848 f:0x1 u:0x000b00)
2018-10-26 18:18:41.347 27827-27876/genesis.com.getlocation D/GraphicBuffer: unregister, handle(0xaaf924c0) (w:848 h:1264 s:848 f:0x1 u:0x000b00)
2018-10-26 18:18:41.347 27827-27876/genesis.com.getlocation D/Surface: Surface::disconnect(this=0x990a1f00,api=1)
2018-10-26 18:18:41.362 27827-27944/genesis.com.getlocation W/SQLiteConnection: Could not change the database journal as DB was busy. Proceeding...
2018-10-26 18:18:41.369 27827-27944/genesis.com.getlocation D/SQLiteDatabase: beginTransaction()
2018-10-26 18:18:41.372 27827-27944/genesis.com.getlocation D/SQLiteDatabase: endTransaction()
2018-10-26 18:18:41.374 27827-27827/genesis.com.getlocation D/WindowClient: Remove from mViews: DecorView@74e9112[], this = android.view.WindowManagerGlobal@fbcfdce
2018-10-26 18:18:41.377 27827-27827/genesis.com.getlocation V/InputMethodManager: onWindowFocus: null softInputMode=32 first=false flags=#81810100
2018-10-26 18:18:41.380 27827-27944/genesis.com.getlocation D/SQLiteDatabase: beginTransaction()
2018-10-26 18:18:41.384 27827-27944/genesis.com.getlocation D/SQLiteDatabase: endTransaction()
2018-10-26 18:18:41.394 27827-27873/genesis.com.getlocation I/BufferQueueProducer: [SurfaceTexture-0-27827-0](this:0xa8461000,id:0,api:1,p:27827,c:27827) queueBuffer: fps=0.67 dur=4486.29 max=3892.05 min=17.56
2018-10-26 18:18:41.403 27827-27945/genesis.com.getlocation D/SQLiteDatabase: beginTransaction()
2018-10-26 18:18:41.405 27827-27945/genesis.com.getlocation D/SQLiteDatabase: endTransaction()
此后,我始终会收到以下日志:(不同之处在于数据库正处于忙碌状态)
2018-10-26 18:18:08.788 27080-27080/genesis.com.getlocation D/ConfirmDialogFragment: onDialogButtonClicked
2018-10-26 18:18:08.788 27080-27080/genesis.com.getlocation D/ConfirmDialogFragmentViewModel: RadioButton value is Desimus
2018-10-26 18:18:08.788 27080-27080/genesis.com.getlocation D/ConfirmDialogFragmentViewModel: Name of place is Khan Plaza
2018-10-26 18:18:08.789 27080-27080/genesis.com.getlocation D/DatabaseController: Location Record Added Successfully
2018-10-26 18:18:08.791 27080-27775/genesis.com.getlocation D/SQLiteDatabase: beginTransaction()
2018-10-26 18:18:08.805 27080-27775/genesis.com.getlocation D/SQLiteDatabase: endTransaction()
2018-10-26 18:18:08.824 27080-27166/genesis.com.getlocation D/Surface: Surface::disconnect(this=0x94b83e00,api=1)
2018-10-26 18:18:08.825 27080-27166/genesis.com.getlocation D/GraphicBuffer: unregister, handle(0xa6043c40) (w:848 h:1264 s:848 f:0x1 u:0x000b00)
2018-10-26 18:18:08.826 27080-27166/genesis.com.getlocation D/GraphicBuffer: unregister, handle(0xa60425c0) (w:848 h:1264 s:848 f:0x1 u:0x000b00)
2018-10-26 18:18:08.826 27080-27166/genesis.com.getlocation D/GraphicBuffer: unregister, handle(0xa6042680) (w:848 h:1264 s:848 f:0x1 u:0x000b00)
2018-10-26 18:18:08.826 27080-27166/genesis.com.getlocation D/Surface: Surface::disconnect(this=0x94b83e00,api=1)
2018-10-26 18:18:08.829 27080-27602/genesis.com.getlocation D/SQLiteDatabase: beginTransaction()
2018-10-26 18:18:08.831 27080-27602/genesis.com.getlocation D/SQLiteDatabase: endTransaction()
2018-10-26 18:18:08.859 27080-27080/genesis.com.getlocation D/WindowClient: Remove from mViews: DecorView@3ad944e[], this = android.view.WindowManagerGlobal@40be950
2018-10-26 18:18:08.867 27080-27080/genesis.com.getlocation V/InputMethodManager: onWindowFocus: null softInputMode=32 first=false flags=#81810100
2018-10-26 18:18:08.879 27080-27235/genesis.com.getlocation I/BufferQueueProducer: [SurfaceTexture-0-27080-1](this:0xa8460000,id:1,api:1,p:27080,c:27080) queueBuffer: fps=0.44 dur=4554.45 max=4532.60 min=21.85
2018-10-26 18:18:08.893 27080-27235/genesis.com.getlocation I/BufferQueueProducer: [SurfaceTexture-0-27080-1](this:0xa8460000,id:1,api:1,p:27080,c:27080) queueBuffer: slot 0 is dropped, handle=0xaaf91800
答案 0 :(得分:0)
很可能您没有成功插入。
确保在插入时获得主ID 确保在后台线程上执行插入操作。 确保您正在后台线程上进行检索
此外,如果您在安装后对模型进行了更改,这将导致其在触摸数据库时失败,因为架构不再与模型变量匹配。
因此,请检查数据库创建代码行,并确保您具有处理架构更改(例如更改模型)的句柄。破坏性的回退很好,如果没有人使用它,则卸载并重新安装。
答案 1 :(得分:0)
检查您的LocationDetails类是否实现了Serializable。 在扩展RoomDatabase的类中,插入要通过这种方式添加的类
@Database(entities = {
LocationDetails.class,
},version = 1, exportSchema = false)
,并确保每次在数据库中添加/删除类时都更新版本号。 我还建议使用room的1.1.1-rc1版本,通过在gradle中添加此行,该版本比1.1.1更加稳定
implementation 'android.arch.persistence.room:runtime:1.1.1-rc1'
我还建议在Android 6+上(可能真的记不起7个)在线程中调用您的请求(设备的evarey AsyncTask共享同一个线程,然后等待执行DoInBackground函数,并且可以)有时最多需要5到10秒才能启动任务。