【问题标题】:How to output result of a SUM SQL query in Appcelerator?如何在 Appcelerator 中输出 SUM SQL 查询的结果?
【发布时间】:2017-11-09 10:36:02
【问题描述】:

下面的查询计算集合中字段(“真实”)的总和,并在控制台中正确显示结果。但是如何使用结果填充文本标签?

var fetchedTranslations = $.translationData;
fetchedTranslations.fetch({
    query: 'SELECT SUM (sum_words) FROM "translationsCollection"'
});
console.log("result: " + JSON.stringify(fetchedTranslations)); //result: [{"SUM (sum_words)":42}]
$.wordCounter.text = ; //should show 42

解决方案

当按照 Larrie 的建议重命名并使用 .toJSON() 方法时,它可以工作

var result = fetchedTranslations.toJSON();
$.wordCounter.text =  result[0].sum_total;    

【问题讨论】:

    标签: database sqlite collections appcelerator


    【解决方案1】:

    将您的查询更改为:

    SELECT SUM (sum_words) AS sum_total FROM "translationsCollection"
    

    然后就可以访问了

    $.wordCounter.text = fetchedTranslations[0].sum_total;
    

    【讨论】:

    • 感谢您的建议。它将键更改为 sum_total 但输出未定义不是对象错误。
    • 我也需要使用 .toJSON 方法。不知道 AS 重命名。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多