【问题标题】:How to properly update values in a sql Database with Appcelerator?如何使用 Appcelerator 正确更新 sql 数据库中的值?
【发布时间】:2017-11-08 10:15:17
【问题描述】:

我正在使用 sql 适配器来处理数据库。处理更新的正确方法是什么?

下面的代码在 questionid 为 1624 的条目的数据库中的 sum_points 字段中正确加了 1 分,但也出现了错误消息 (rs.fieldCount) 并且最后的 console.log 没有执行

var fetchedTranslations = $.translationData;
fetchedTranslations.fetch({
query: { statement: 'UPDATE "translationsCollection" SET "sum_points" = "sum_points" + ? WHERE "questionid" = ?', params: [1,1624] } //correctly changes the value but results in error message (rs.fieldCount)
});
console.log("fetchedTranslations: " + JSON.stringify(fetchedTranslations));
}

views.xml:

 <Alloy>
<!--<Model src="translationsCollection"/>-->
<Collection src="translationsCollection" instance="true" id="translationData"/>
<Window id="learnQuestions" title="Learn">
<View id="learnQuestionContainer" layout="vertical" width="98%" borderWidth="2" borderRadius="5" backgroundColor="Alloy.CFG.design.backgroundColor" borderColor="#E6000000" height="98%">
    <View id="ratingBar" height="Ti.UI.SIZE" backgroundColor="orange" left="1%" width="98%">
        <Label id="pointsCounter" width="30%" left="30%" height="Ti.UI.SIZE"  top="0" text="pointsCounter" onClick="calculatePoints" top="0"/> 
        <!-- calculatePoints triggers the points update function-->
    </View>
    <ScrollView id="learnQuestionsContainer" layout="vertical" height="Ti.UI.SIZE" dataCollection="$.translationData" dataTransform="transformFunction">
    </ScrollView>
</View>
</Window>

trasnlationCollection.js(在模型文件夹中)

exports.definition = {
config: {
    columns: {
        "questionid": "real",
        "question": "text",
        "answer": "text",
        "difficulty": "real",
        "language": "text",
        "sum_points": "real",
        "sum_words": "real"
    },
    adapter: {
        type: "sql",
        collection_name: "translationsCollection",
        idAttribute : "questionid"
    }
},
extendModel: function(Model) {
    _.extend(Model.prototype, {
        idAttribute : "questionid",
        // extended functions and properties go here
    });
    return Model;
},

滚动视图中的标签(由数据库填充)以编程方式创建。

解决方案

在 .xml 文件中添加模型 src 并按照 Rene 的建议获取和设置值:

<Model src="translationsCollection"/> 

var fetchedTranslations = $.translationData;
var model = fetchedTranslations.get('1624');
console.log("model fetched: " + JSON.stringify(model));
model.set({sum_points: model.get('sum_points') + 1});
model.save();

【问题讨论】:

    标签: database sqlite appcelerator


    【解决方案1】:

    您似乎在使用集合和模型,而不仅仅是数据库。对于最重要的事情,您根本不需要使用查询。只需查看有关模型的相关docs

    在您的情况下,只需获取具有正确 ID 的模型(或任何获取它们的主干方法)

    var model = $.fetchedTranslations.get('123');
    model.set({sum_points: model.get('sum_points') + 1});
    model.save();
    

    由于集合是基于主干的,我建议也检查backbone docs,这样您就可以了解如何通过 ID、过滤器等以外的任何其他属性获取。

    【讨论】:

    • 感谢您的澄清。当我尝试此代码(和 $.translationData.get('1624') 时,其中 1624 是 questionid console.log 输出为模型未定义。questionid 在适配器和模型(在 model.js)文件中设置为 idAttribute。
    • 你是如何定义这个系列的?您用$. 指代它,但通常是实例集合,而不是全局集合。
    • 我已经添加了集合 src。我花了很多时间在关于模型和集合的官方文档上。不幸的是,我很难理解它是如何协同工作的。像这样的文档更有用:medium.com/all-titanium/… ;)
    • 很高兴看到我的文档正在被阅读。您将需要仅使用集合名称而不是 ID 来更改对集合的引用
    • 谢谢。我已根据您的建议添加了解决方案。我之前也没有将 Model src 添加到集合中。
    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多