由于appcelerator中的错误字符串连接而导致参数化执行查询中断

时间:2016-02-18 15:42:55

标签: mysql string sqlite appcelerator appcelerator-titanium

我正在尝试使用Appcelerator API使用db.execute执行INSERT查询。根据其文档,它应该理想地用作:

var thisName = 'Arthur';
var thisPhoneNo = '1-617-000-0000';
var thisCity = 'Mountain View';
db.execute('INSERT INTO people (name, phone_number, city) VALUES (?, ?, ?)', thisName, thisPhoneNo, thisCity);

Database execute API doc for Appcelerator

所以这是我的问题代码几乎让我眼花缭乱:

    var _tbl = "g_work_docket";
    var records = [{
        "id" : "2134",
        "accession_no" : "20043446",
        "opened" : "2014-07-25",
        "job_origin" : "TRAMS Inspection",
        "deadline" : "2014-09-30",
        "completed_by" : "naren",
        "completed" : "2016-02-18",
        "mitigation_option" : "",
        "location_no" : "186:03:",
        "notes" : "",
        "comments" : "",
        "priority" : null,
        "status" : "closed",
        "is_approved" : "0",
        "reviewer_comments" : "",
        "updated_at" : "2016-02-18 12:58:50",
        "is_deleted" : "0",
        "site" : "K"
    }, {
        "id" : "3016",
        "accession_no" : "196920850",
        "opened" : "2000-01-19",
        "job_origin" : "TRAMS Inspection",
        "deadline" : "2001-01-01",
        "completed_by" : "naren",
        "completed" : "2016-02-18",
        "mitigation_option" : "",
        "location_no" : "770:01:",
        "notes" : "Further inspection :\n\nDecay assesment : microdrill trunk base",
        "comments" : "",
        "priority" : null,
        "status" : "closed",
        "is_approved" : "0",
        "reviewer_comments" : "",
        "updated_at" : "2016-02-18 13:26:14",
        "is_deleted" : "0",
        "site" : "W"
    }];

    _.each(records, function(record) {
        var qry = "INSERT OR REPLACE INTO " + _tbl + " (";
        _.each(record, function(value, key, list) {
            qry += '' + key + ',';
        });
        qry = qry.slice(0, -1);
        qry += ") VALUES (";
        _.each(record, function(value, key, list) {
            qry += '?,';
        });
        qry = qry.slice(0, -1);
        qry += "),";
        _.each(record, function(value, key, list) {
            qry += "'" + value + "',";
        });
        qry = qry.slice(0, -1);
        db.execute(qry);
    });

这总是错误说:

[ERROR] :  Error: near "'2134'": syntax error (code 1): , while compiling: INSERT OR REPLACE INTO g_work_docket (id,accession_no,opened,job_origin,deadline,completed_by,completed,mitigation_option,location_no,notes,comments,priority,status,is_approved,reviewer_comments,updated_at,is_deleted,site) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?),'2134','20043446','2014-07-25','TRAMS Inspection','2014-09-30','naren','2016-02-18','','186:03:','','','null','closed','0','','2016-02-18 12:58:50','0','K'

1 个答案:

答案 0 :(得分:1)

通过首先将值推送到数组中然后将其传递给db.execute函数的第2个参数来传递值来解决此问题。

    _.each(records, function(record) {
        var qry = "INSERT OR REPLACE INTO " + _tbl + " (";
        var dataValues = [];                
        _.each(record, function(value, key, list) {             
            qry += '' + key + ','; 
        });
        qry = qry.slice(0, -1);
        qry += ") VALUES (";
        _.each(record, function(value, key, list) {
            qry += '?,';
        });
        qry = qry.slice(0, -1);
        qry += ")";                 
        _.each(record, function(value, key, list) {             
            dataValues.push(value);
        });                             
        db.execute(qry, dataValues);
    });