问题
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