7

我想在我的 Datetime 字段中保存 Date For insert 我没有问题...我尝试了很多尝试,这是最后一次但结果是一样的

插入:

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
4

4 回答 4

17

在@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) {
于 2013-08-02T17:57:44.097 回答
15

由于您正在使用当前时间戳,因此您可能只考虑使用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
于 2013-08-02T18:01:44.943 回答
5

日期需要包含在其中"'以便 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'

于 2013-08-02T17:48:30.333 回答
0
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;
    }

在更新上做同样的事情,

于 2015-07-22T15:35:18.810 回答