2012-08-24 14 views
5

त्रुटि लॉग:आईएसओ 8601 टाइमस्टैम्प: MySQL गलत datetime मूल्य

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

मैं कोड के इस टुकड़े का उपयोग अपने Node.js

if s instanceof Date 
     return s.toISOString() 

में और उन्हें डेटाबेस में अपडेट किया गया।

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

मैंने कुछ गलत कर रहा हूँ? मैंने सर्वर में किसी तालिका से PHPMyAdmin का उपयोग करके एक तालिका कॉपी की है।

बहुत बहुत धन्यवाद।

उत्तर

9

रूप 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” .

आपका तारीख '2012-08-24T17:29:11.683Z' का शाब्दिक इन स्वरूपों में से किसी से मेल नहीं खाती; आप सुझाव है कि या तो —

  • उपयोग बजाय Node.js दिनांक वस्तु की toLocaleFormat() विधि (यकीन है कि MySQL कनेक्शन के समय क्षेत्र से मेल खाता है कि Node.js किसी भी स्थान पर हो):

    if s instanceof Date 
         return s.toLocaleFormat("%Y-%m-%d %H:%M:%S") 
    
  • का उपयोग यूनिक्स युग के बाद मिलीसेकंड में समय का मूल्य प्राप्त करने के लिए विधि, यूनिक्स युग के बाद (सेकेंड प्राप्त करने के लिए) और मेरे माध्यम से गुजरने के लिए एसक्यूएल का FROM_UNIXTIME() फ़ंक्शन।

+0

यह 'MySQL' के विभिन्न संस्करणों के साथ बदल गया फ़ॉर्मेट करने के लिए क्षमता के साथ node.js प्रदान कर सकता है? मुझे यकीन है कि जिस सर्वर पर मैंने तालिका की प्रतिलिपि बनाई है वह ठीक से चल रहा है। –

+0

@ComboZhc: मेरे ज्ञान के लिए नहीं। शायद PHPMyAdmin इस तरह से प्रदर्शन के लिए आउटपुट स्वरूपित करता है, लेकिन प्रारूप मान्य MySQL डेटाटाइम शाब्दिक नहीं है। यदि आवश्यक हो, तो रूपांतरणों को करने के लिए आप ['STR_TO_DATE()'] (http://dev.mysql.com/doc//en/date-and-time-functions.html#function_str-to-date) का उपयोग कर सकते हैं। – eggyal

+0

यदि आपको मिलीस और/या माइक्रोसेकंड के साथ डेटटाइम सेट करने में समस्याएं हैं तो आप mysql सख्त मोड को अक्षम कर सकते हैं (sql_mode = '' सेट करें) – momo

2

मैं इस लिंक पर इसे खोजने:

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

ऐसा लगता है कि एक ISO8601 टाइमस्टैम्प डालने सुरक्षित नहीं है। यह MySQL के पार्सर पर निर्भर करता है। शायद विभिन्न संस्करण विभिन्न विधियों का उपयोग करें।

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 

यह टुकड़ा कोड एक Date