如何用合金数据库更新记录

时间:2016-08-02 08:14:41

标签: javascript backbone.js titanium

我正在使用带合金数据库的AppceleratorStudio构建应用程序。我还使用了骨干来访问数据。

如果我尝试更新数据库上的数据,那么我有一个问题。 这是我的代码:

var _model = Alloy.createModel("AlertDAO", {
    ID: null,
    IdOmnia : obj.id_omnia,
        Code: obj.info.code,
        CodeSystem: obj.info.code_system,
        DisplayName: obj.info.displayName,
        DisplayNameTarget: obj.info.displayNameTarget,
        StartDate: obj.date_start,
        EndDate: obj.date_end,
        CodeStatus: obj.alarm_alert_info.code,
        CodeSystemStatus: obj.alarm_alert_info.code_system,
        DisplayNameStatus: obj.alarm_alert_info.displayName,
        DisplayNameTargetStatus: obj.alarm_alert_info.displayNameTarget,
        Observation: ""           
});
//I check if the recors exist on database
var alert = db_alert.getAlert(null,_model.attributes.IdOmnia);

if(alert!=undefined){
    //update
    _model.attributes.ID = alert.attributes.ID;
    Titanium.API.info("MODEL DOPO AGGIORNAMENTO ID: "+_model.attributes.ID);
    _model.save();
    Titanium.API.info("ALERT AGGIORNATO con ID: "+ _model.attributes.ID);
}else{
    //sul database non esite.
    _model.save();
    Titanium.API.info("ALERT SALVATO con ID: "+ _model.attributes.ID);
}

使用此代码我创建一个" _model"对于数据,接下来我检查该ID是否也保存在本地数据库中。如果只是保存,我应该更新数据。

然后问题在于该行代码:

 _model.attributes.ID = alert.attributes.ID;
    Titanium.API.info("MODEL DOPO AGGIORNAMENTO ID: "+_model.attributes.ID);
    _model.save();
    Titanium.API.info("ALERT AGGIORNATO con ID: "+ _model.attributes.ID);

第一条消息打印ID = 1,保存后,ID = 2 grrrr

如何更新所有字段但不添加下一行,但更新行?

这是模特。

exports.definition = {
    config: {
        columns: {
            "ID": "INTEGER PRIMARY KEY AUTOINCREMENT",
            "IdOmnia": "INTEGER",
            "Code": "text",
            "CodeSystem": "text",
            "DisplayName": "text",
            "DisplayNameTarget": "text",
            "StartDate": "text",
            "EndDate": "text",
            "CodeStatus": "text",
            "CodeSystemStatus": "text",
            "DisplayNameStatus": "text",
            "DisplayNameTargetStatus": "text",
            "Observation": "text"
        },
        adapter: {
            type: "sql",
            collection_name: "AlertDAO",
            idAttribute: "ID"
        }
    },
    extendModel: function(Model) {
        _.extend(Model.prototype, {
            // extended functions and properties go here
        });

        return Model;
    },
    extendCollection: function(Collection) {
        _.extend(Collection.prototype, {
            destroyAll : function(opt) {
              var db = Ti.Database.open(this.config.adapter.db_name);
              db.execute("DELETE FROM " + this.config.adapter.collection_name);
              db.close();
              this.models = [];
              if (!opt || !opt.silent) { this.trigger("reset"); }
              return this;
            }
        });

        return Collection;
    }
};

0 个答案:

没有答案