Issue with update query in titanium alloy

半世苍凉 提交于 2019-12-10 23:56:58

问题


I'm new to titanium and alloy framework. I have created the models to store some data. I am able to insert and retrieve the data. I don't know How to update the row.

As suggest I did as follows,

var userLang = Alloy.Collections.userLanguage;   
var models = Alloy.createModel("userLanguage");
    models.fetch({id: 1});
    models.save({
       languageID: langID,
       languageText: lang
    });

The above code s not showing any error, But when I try to select a row from the table,

var userLang = Alloy.createModel('userLanguage');
 userLang.fetch({
        query: {
            statement: "SELECT * FROM userLanguage WHERE id = ?;",
            params: [ 1 ]
        }
    });
 alert("Updated value : "+userLang.get("languageText"));

Model

exports.definition = {
    config : {
        "columns": {
            "id": "integer",
            "languageID": "integer",
            "languageText": "text"
        },
        "adapter": {
            "type": "sql",
            "collection_name": "userLanguage"
        }
    }
};

The selected row is not updated as expected


回答1:


...I need titanium query...

I guess that you are talking about Backbone. If so then below you can see an example how to update a model.

You can find more informations here: http://docs.appcelerator.com/titanium/latest/#!/guide/Alloy_Sync_Adapters_and_Migrations

var model = Alloy.createModel("userLanguage");

model.fetch({id: 1});

model.save({
   languageID: langID,
   languageText: lang
});



回答2:


to update a model you should first get it from the collection

var userLang = Alloy.Collections.userLanguage; 
userLang.fetch();
var langModel=userLang.get({
            id:1
     });

then use set to modify model's properties

langModel.set({
      languageText:'new value'
});

then save it

langModel.save();

You should not update the id if you put it as the primary key ... You should fetch data before getting any model & after updating them that's it.



来源:https://stackoverflow.com/questions/26333242/issue-with-update-query-in-titanium-alloy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!