mUserDao.deleteByKey(l);
允許您定義屬性映射到的非默認列名。如果不存在,GreenDAO將以SQL-ish方式使用字段名稱(大寫,下劃線而不是camel情況,例如 name將成爲 NAME)。注意:您當前只能使用內聯常量來指定列名。
GreenDao的升級思路:
- 創建臨時表TMP_,複製原來的數據庫到臨時表中;
- 刪除之前的原表;
- 創建新表;
- 將臨時表中的數據複製到新表中,最後將TMP_表刪除掉;
Android GreenDAO 3.0 不修改版本號的情況下增加、刪除表、添加字段
protected void updateInsideSynchronized(T entity, DatabaseStatement stmt, boolean lock) {
// To do? Check if it's worth not to bind PKs here (performance).
bindValues(stmt, entity);
int index = config.allColumns.length + 1;
K key = getKey(entity);
if (key instanceof Long) {
stmt.bindLong(index, (Long) key);
} else if (key == null) {
throw new DaoException("Cannot update entity without key - was it inserted before?");
} else {
stmt.bindString(index, key.toString());
}
stmt.execute();
attachEntity(key, entity, lock);
}
protected void updateInsideSynchronized(T entity, SQLiteStatement stmt, boolean lock) {
// To do? Check if it's worth not to bind PKs here (performance).
bindValues(stmt, entity);
int index = config.allColumns.length + 1;
K key = getKey(entity);
if (key instanceof Long) {
stmt.bindLong(index, (Long) key);
} else if (key == null) {
throw new DaoException("Cannot update entity without key - was it inserted before?");
} else {
stmt.bindString(index, key.toString());
}
stmt.execute();
attachEntity(key, entity, lock);
}
每一個實體bean都應該有一個主鍵
如果沒有設置主鍵,那麼實體bean不能更新,更新時會提示
does not have a single-column primary key
not have a single-column primary key
QueryBuilder的使用
編寫SQL可能很困難並且容易出現錯誤,這些錯誤僅在運行時纔會被注意到。該QueryBuilder的類可以讓你建立你的實體,而不SQL自定義查詢,並有助於在編譯時已檢測錯誤。