2012-06-14 67 views
5
所有列

我有以下的數據庫表對象:更新在ormlite數據庫表中的Android

public class Goal { 
    @DatabaseField(generatedId = true) 
    private int id; 
    @DatabaseField 
    private String goal_title; 
    @DatabaseField 
    private String goal_desc; 
    @DatabaseField 
    private String goal_why; 
    ... 
} 

我增加了一些行到該表,現在我想編寫一個查詢更新的所有列在這張表中排。我已經看過ORM的文檔,不知道如何編寫這個查詢。請幫助我如何編寫此查詢。

回答

17

我認爲你需要RTFM。我花了很長時間在ORMLite文檔上,我認爲它covers the UpdateBuilder相當不錯。隨意問更具體的問題,如果沒有,我可以添加更多的細節。

要從文檔引用:

的DAO也可用於構建自定義UPDATE和DELETE語句。更新語句用於更改表中與WHERE模式匹配的某些行中的某些字段 - 如果沒有where(),則更新所有行。刪除語句用於從表中刪除與WHERE模式相匹配的行 - 如果沒有where(),則刪除所有行。

要調整示例代碼使用您Goal對象:

UpdateBuilder<Goal, Integer> updateBuilder = goalDao.updateBuilder(); 
// update the goal_title and goal_why fields 
updateBuilder.updateColumnValue("goal_title", "some other title"); 
updateBuilder.updateColumnValue("goal_why", "some other why"); 
// but only update the rows where the description is some value 
updateBuilder.where().eq("goal_desc", "unknown description"); 
// actually perform the update 
updateBuilder.update(); 

希望這有助於。

+0

非常感謝您灰色的回覆,我剛剛嘗試了這段代碼,但它在最後一行 updateBuilder.update()上發生錯誤,updateBuilder類型不存在該更新方法。還有兩個其他方法updateColumnExpression和updateColumnValue,但不是更新方法。請幫助,如果你得到我的問題。再次感謝 – Munazza

+0

這是在4.34版本中添加的。對於'goalDao.update(updateBuilder.prepare());' – Gray

+0

goalDao.update(updateBuilder.prepare());也沒有工作:(,並再次給出一個錯誤,這是不確定的類型updateBuilder。我有ormlite 4.30,我應該使用4.34? – Munazza