如何使用Room数据库执行sql PRAGMA语句?
为什么这句话不起作用?
[http]
# Determines whether HTTP endpoint is enabled.
enabled = true
# The bind address used by the HTTP service.
bind-address = ":8086"
[[udp]]
enabled = false
bind-address = ":8089"
答案 0 :(得分:0)
如何使用Room数据库执行sql PRAGMA语句?
您可以覆盖@Database类的 init 方法,并在配置之前执行编译指示(或其他您希望绕过Room' s控制的数据库操作)。
e.g。 : -
@Override
public void init(@NonNull DatabaseConfiguration configuration) {
doPreRoomOpenStuff(configuration);
super.init(configuration);
}
private void doPreRoomOpenStuff(DatabaseConfiguration dbconfig) {
String dbpath = (dbconfig.context).getDatabasePath(DBNAME).getPath();
if (ifDBExists(dbpath)) {
SQLiteDatabase db = SQLiteDatabase.openDatabase(dbpath, null,Context.MODE_PRIVATE);
Cursor csr = db.rawQuery("PRAGMA wal_checkpoint",null);
while (csr.moveToNext()) {
StringBuilder sb = new StringBuilder();
for (int c = 0; c < csr.getColumnCount(); c++) {
sb.append("\n\tColumnName = ").append(csr.getColumnName(c)).append(" Value=").append(csr.getString(c));
}
Log.d("INFO",sb.toString());
}
db.close();
}
}
private boolean ifDBExists(String dbpath) {
File db = new File(dbpath);
if(db.exists()) return true;
File dir = new File(db.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
return false;
}
示例结果: -
06-18 18:22:14.244 1875-1875/? D/INFO: ColumnName = busy Value=0
ColumnName = log Value=-1
ColumnName = checkpointed Value=-1
为什么这句话不起作用?
我不确定,但我认为房间很容易控制。
答案 1 :(得分:0)
我遇到了这个问题,发现解决方案是检查查询结果。 我刚刚检查了第一列int,如果参数为FULL,RESTART或TRUNCATE并且调用被阻止(see this.),则它将为1。
Cursor c = room.query(new SimpleSQLiteQuery("pragma wal_checkpoint(full)"));
if(c.moveToFirst() && c.getInt(0) == 1)
throw new RuntimeException("Checkpoint was blocked from completing");