2012-08-24 31 views
5

Lỗi đăng nhập:ISO 8601 Timestamp cho cơ sở dữ liệu MySQL: MySQL giá trị datetime sai

{ [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] }

tôi sử dụng đoạn mã này trong tôi Node.js

if s instanceof Date 
     return s.toISOString() 

và cập nhật chúng trong cơ sở dữ liệu.

Khái niệm SQL chèn sau:

 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\') 

Tôi có làm điều gì sai trái? Tôi vừa sao chép một bảng bằng cách sử dụng PHPMyAdmin từ một bảng trong máy chủ.

Cảm ơn rất nhiều.

Trả lời

9

Như đã nêu trong 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” .

ngày của bạn đen của '2012-08-24T17:29:11.683Z' không phù hợp với bất kỳ các định dạng; đề nghị bạn hoặc —

  • sử dụng thay vì phương pháp toLocaleFormat() các Node.js ngày của đối tượng (hãy chắc chắn rằng múi giờ của kết nối MySQL khớp với các locale Node.js của):

    if s instanceof Date 
         return s.toLocaleFormat("%Y-%m-%d %H:%M:%S") 
    
  • sử dụng Phương thức valueOf() của đối tượng ngày tháng để lấy giá trị của thời gian trong mili giây từ kỷ nguyên UNIX, chia cho 1000 (để có được giây từ kỷ nguyên UNIX) và đi qua My Hàm FROM_UNIXTIME() của SQL.

+0

Nó đã thay đổi qua các phiên bản khác nhau của 'MySQL'? Tôi chắc chắn rằng trên máy chủ mà tôi sao chép bảng từ đang chạy đúng cách. –

+0

@ComboZhc: Không phải với kiến ​​thức của tôi. Có lẽ PHPMyAdmin định dạng đầu ra để hiển thị theo cách đó, nhưng định dạng không phải là một chữ thời gian datetime hợp lệ của MySQL. Bạn có thể sử dụng ['STR_TO_DATE()'] (http://dev.mysql.com/doc//en/date-and-time-functions.html#function_str-to-date) để thực hiện chuyển đổi, nếu được yêu cầu. – eggyal

+0

Nếu bạn gặp sự cố khi đặt thời gian biểu bằng millis và/hoặc micro giây, bạn có thể tắt chế độ nghiêm ngặt mysql (đặt sql_mode = '') – momo

2

Tôi tìm thấy nó trên liên kết này:

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

Dường như chèn một dấu thời gian ISO8601 là không an toàn. Nó phụ thuộc vào trình phân tích cú pháp của MySQL. Có thể các phiên bản khác nhau sử dụng các phương pháp khác nhau.

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 

này đang mảnh có thể cung cấp node.js với khả năng định dạng một Date

Các vấn đề liên quan