2012-08-24 14 views
5
registro

Errore:ISO 8601 timestamp per MySQL Database: MySQL valore datetime non corretto

{ [Error: Incorrect datetime value: '2012-08-24T17:29:11.683Z' for column 'robot _refreshed_at' at row 1] number: 1292, sqlStateMarker: '#', sqlState: '22007', message: 'Incorrect datetime value: \'2012-08-24T17:29:11.683Z\' for column \' robot_refreshed_at\' at row 1', sql: 'INSERT INTO users (id,name,count_moments,count_likes,count_followers,rob ot_refreshed_at,robot_count_followers) VALUES (\'1834084\',\'NNNyingzi\',\'5\',\ '0\',\'0\',\'2012-08-24T17:29:11.683Z\',\'0\')', setMaxListeners: [Function], emit: [Function], addListener: [Function], on: [Function], once: [Function], removeListener: [Function], removeAllListeners: [Function], listeners: [Function] }

Io uso questo pezzo di codice nel mio Node.js

if s instanceof Date 
     return s.toISOString() 

e aggiornato nel database.

L'espressione SQL inserto segue:

 INSERT INTO users (id,name,count_moments,count_likes,count_followers,rob ot_refreshed_at,robot_count_followers) VALUES (\'1834084\',\'NNNyingzi\',\'5\',\ '0\',\'0\',\'2012-08-24T17:29:11.683Z\',\'0\') 

Sto facendo qualcosa di sbagliato? Ho appena copiato una tabella utilizzando PHPMyAdmin da una tabella nel server.

Grazie mille.

risposta

9

come indicato in Date and Time Literals:

MySQL recognizes DATETIME and TIMESTAMP values in these formats:

  • As a string in either 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD HH:MM:SS' format. A “relaxed” syntax is permitted here, too: Any punctuation character may be used as the delimiter between date parts or time parts. For example, '2012-12-31 11:30:45' , '2012^12^31 11+30+45' , '2012/12/31 11*30*45' , and '[email protected]@31 11^30^45' are equivalent.

  • As a string with no delimiters in either 'YYYYMMDDHHMMSS' or 'YYMMDDHHMMSS' format, provided that the string makes sense as a date. For example, '20070523091528' and '070523091528' are interpreted as '2007-05-23 09:15:28' , but '071122129015' is illegal (it has a nonsensical minute part) and becomes '0000-00-00 00:00:00' .

  • As a number in either YYYYMMDDHHMMSS or YYMMDDHHMMSS format, provided that the number makes sense as a date. For example, 19830905132800 and 830905132800 are interpreted as '1983-09-05 13:28:00' .

A DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision. Although this fractional part is recognized, it is discarded from values stored into DATETIME or TIMESTAMP columns. For information about fractional seconds support in MySQL, see Section 11.3.6, “Fractional Seconds in Time Values” .

La tua data letterale '2012-08-24T17:29:11.683Z' non si adatta qualsiasi di questi formati; suggerire o —

  • uso invece toLocaleFormat() metodo Data dell'oggetto Node.js (essere sicuri che il fuso orario della connessione MySQL corrisponde a quello del locale di Node.js):

    if s instanceof Date 
         return s.toLocaleFormat("%Y-%m-%d %H:%M:%S") 
    
  • utilizzare il Metodo valueOf() dell'oggetto Data Node.js per ottenere il valore del tempo in millisecondi dall'epoca UNIX, dividere per 1000 (per ottenere secondi dall'epoca UNIX) e passare attraverso My La funzione di SQL FROM_UNIXTIME().

+0

è cambiata nel corso diverse versioni di 'MySQL'? Sono sicuro che sul server in cui ho copiato la tabella da sta funzionando correttamente. –

+0

@ComboZhc: non a mia conoscenza. Forse PHPMyAdmin formatta l'output per la visualizzazione in quel modo, ma il formato non è un letterale valido per il datetime di MySQL. Puoi utilizzare ['STR_TO_DATE()'] (http://dev.mysql.com/doc//en/date-and-time-functions.html#function_str-to-date) per eseguire conversioni, se necessario. – eggyal

+0

Se si verificano problemi con l'impostazione di data e ora con millis e/o microsecondi, è possibile disabilitare la modalità strict mysql (set sql_mode = '') – momo

2

Trovo su questo link:

MySQL insert to DATETIME: is it safe to use ISO::8601 format?

Sembra che l'inserimento di un timestamp ISO8601 non è sicuro. Dipende dal parser di MySQL. Forse versioni diverse usano metodi diversi.

Date.prototype.format = (format) -> 
    o = { 
    "(M+)" : this.getMonth()+1, 
    "(d+)" : this.getDate(), 
    "(h+)" : this.getHours(), 
    "(m+)" : this.getMinutes(), 
    "(s+)" : this.getSeconds(), 
    "(q+)" : Math.floor((this.getMonth()+3)/3), 
    "(S)" : this.getMilliseconds() 
    } 
    if /(y+)/.test(format) 
    format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)) 
    for k, v of o 
    if (new RegExp(k)).test(format) 
     format = format.replace(RegExp.$1, if RegExp.$1.length == 1 then o[k] else ('00'+ o[k]).substr((''+ o[k]).length)) 
    return format 

Questo codice pezzo potrebbe fornire node.js con la possibilità di formattare un Date