2

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

Vinod
  • 2,263
  • 9
  • 55
  • 104

2 Answers2

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
});
0101
  • 2,697
  • 4
  • 26
  • 34
  • The above code is not working. Can you please suggest work around. We have set the values in above the above answer right – Vinod Oct 14 '14 at 09:53
  • Updated my question with original code. What's wrong with code. Please suggest some solution – Vinod Oct 14 '14 at 12:11
  • Add `"idAttribute": "id"` to the "adapter" in your Model. – 0101 Oct 14 '14 at 16:33
  • I tried your suggestion, But I am not able to update the row. Can you please show me an example. Please – Vinod Oct 16 '14 at 16:46
  • Have you got `idAttribute` in the `adapter`? – 0101 Oct 16 '14 at 21:36
  • Yeah, I have Added "idAttribute": "id" to the "adapter" in Model. It is updating one time. But from next time it is not updating. When I select the updated value I'm getting un defined value – Vinod Oct 17 '14 at 11:07
  • I have tried it and it is working without any problem. Make sure that you are not getting a warning (`No config.adapter.idAttribute specified for table XXX`) also try to delete whole database (or just particular table) and start from beginning. – 0101 Oct 17 '14 at 13:23
  • I have only one row in my table, I just need to update the same row and select the same. For updating and selecting I'm using the id in where condition. Can you please suggest me how can I select that row after update event happens – Vinod Oct 17 '14 at 14:27
0

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.

LHIOUI
  • 3,287
  • 2
  • 24
  • 37
  • I'm getting the following error, Uncaught TypeError Cannot call method 'set' of undefined – Vinod Oct 14 '14 at 14:41
  • Edited my question by adding model – Vinod Oct 14 '14 at 16:12
  • This is completely wrong. You don't have to fetch whole Collection to update only one Model. Also if you want to get a Model you can use just `get(id)`. To do `set` and then `save` is not required as well. – 0101 Oct 14 '14 at 16:37
  • I'm not clear Can you please update your answer please – Vinod Oct 14 '14 at 16:39
  • I tried your suggestion, But I am not able to update the row. Can you please show me an example. Please – Vinod Oct 16 '14 at 16:45