2017-08-01 61 views
0
單列計算SUM

我使用的是最新版本的DBFLOW庫,我嘗試計算總和單列作爲countOfNewPosts與DbFlow

這是我的表:

public class CafeWebNewPostInformation extends BaseModel { 
    @PrimaryKey 
    @Column 
    public int id; 

    @Column 
    public int cafeWebServerId; 

    @Column 
    public int countOfNewPosts; 

    @Column 
    @ForeignKey(tableClass = AlachiqCafeWebs.class, 
      references = @ForeignKeyReference(columnName = "parentId", foreignKeyColumnName = "id")) 
    private int parentId; 

    @Column 
    public int lastSavedId; 

    @Column 
    public int lastRequestedId; 

    @Column 
    public int lastRetrievedId; 
} 

,這是我的代碼來獲得,從這個表:

CafeWebNewPostInformation countOfNewPost = 
     SQLite.select(
       Method.sum(CafeWebNewPostInformation_Table.countOfNewPosts).as("count") 
     ) 
       .from(CafeWebNewPostInformation.class).querySingle(); 

我沒有得到任何錯誤,這是這一結果:

截圖:

enter image description here

發生什麼和在哪裏count這個結果?

回答

0

問題解決

想一想。您正在致電

SELECT SUM(`countOfNewPosts`) as 'count' FROM `CafeWebNewPostInformation` 

意味着您返回的結果僅包含該查詢的單行計數。你不會得到任何錯誤,因爲我們沒有在光標上實現嚴格的列匹配。您需要定義一個@QueryModelQueryModels已經調用數的單場:

@QueryModel // (add database reference) 
public class CountOfPost { 

    @Column 
    public int count; 
} 

然後

CountOfPost count = SQLite.select(
    Method.sum(CafeWebNewPostInformation_Table.countOfNewPosts).as("count")) 
    .from(CafeWebNewPostInformation.class) 
    .queryCustomSingle(CountOfPost.class); 

還是可以的呼叫querySingle()通話count(),而不是你。

reference