【问题标题】:Save DateTime mysql with nodejs用nodejs保存日期时间mysql
【发布时间】:2013-08-04 01:21:15
【问题描述】:

我想在我的日期时间字段中保存日期 对于插入我没有问题... 我尝试了很多次,这是最后一次但结果是一样的

插入:

var now = new Date();
var jsonDate = now.toJSON();
var then = new Date(jsonDate);


var o_voto_foto = {
    profilo_id: profilo,
    foto_id: data.id_foto_votata,
    punteggio: data.voto,
    proprietario_id: data.proprietario_foto,
    created: then
};


connection.query('INSERT INTO prof_voto_foto SET ?', o_voto_foto, function(error, rows) {
    if (error) {
        var err = "Error on INSERT 'votoFotoFancybox': " + error;
        console.error(err);
        throw err;
    }

更新表格:

var now = new Date();
var jsonDate = now.toJSON();
var then = new Date(jsonDate);

connection.query('UPDATE prof_voto_foto SET punteggio = ' + data.voto + ', created = ' + then +' WHERE profilo_id = ' + profilo + ' AND foto_id = ' + data.id_foto_votata, function(error, rows) {
    if (error) {
        var err = "Error on UPDATE 'votoFotoFancybox': " + error;
        console.error(err);
        throw err;
    }

我收到此错误:

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Aug 02 2013 19:03:13 GMT+0200 (CEST) WHERE profilo_id = 103 AND foto_id = 5' at line 1

【问题讨论】:

  • 除非您使用参数化查询,否则不要忘记对您的所有值调用connection.escape

标签: javascript mysql node.js


【解决方案1】:

日期需要包含在"' 中,以便mysql 知道它正在处理一个字符串并可以将其转换为日期时间。

UPDATE prof_voto_foto SET punteggio = ' + data.voto + ', created = "' + then +'" WHERE profilo_id = ' + profilo + ' AND foto_id = ' + data.id_foto_votata

它可能需要格式化以匹配 mysql 的预期,Aug 02 2013 19:03:13 GMT+0200 (CEST) 需要转换为 'YYYY-MM-DD HH:MM:SS'

【讨论】:

    【解决方案2】:

    在@tadman 的帮助下,它成功了

    created = new Date();
    connection.query('UPDATE prof_voto_foto SET punteggio = ' + connection.escape(data.voto) + ', created = ' + connection.escape(created) + ' WHERE profilo_id = ' + connection.escape(profilo) + ' AND foto_id = ' + connection.escape(data.id_foto_votata), function(error, rows) {
    

    【讨论】:

    • 对于 Date() 对象是否需要 connection.escape()
    • 我同意@anshabhi 的观点,您不需要转义未从用户输入中传入的对象。对于上面创建的日期,此处不需要转义。
    【解决方案3】:

    由于您使用的是当前时间戳,您可以考虑使用NOW() MySQL 函数,而不是从 javascript 填充日期。当然,这意味着您将标准化 MySQL 服务器时间上的所有时间戳,而不是可能需要也可能不需要的客户端时间。

    用法如下:

    'UPDATE prof_voto_foto SET punteggio = ' + data.voto + ', created = NOW() WHERE profilo_id = ' + profilo + ' AND foto_id = ' + data.id_foto_votata
    

    【讨论】:

    • 我更喜欢这种方式,而不是搞乱 JSON 日期
    【解决方案4】:
    var o_voto_foto = {
        profilo_id: profilo,
        foto_id: data.id_foto_votata,
        punteggio: data.voto,
        proprietario_id: data.proprietario_foto,
        // just delete the stuff you add here :) then add created = now() on the sql statement. that should do the trick
    };
    
    
    connection.query('INSERT INTO prof_voto_foto SET **created = now(),** ?', o_voto_foto, function(error, rows) {
        if (error) {
            var err = "Error on INSERT 'votoFotoFancybox': " + error;
            console.error(err);
            throw err;
        }
    

    对更新做同样的事情,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-05
      • 1970-01-01
      • 2021-08-19
      • 2011-09-25
      • 2017-08-25
      相关资源
      最近更新 更多