hello everyond, my name is Sorn and i need your help. i have problem about save datetime (Dec 10 2020 17:26:34 GMT+0700 (Indochina Time)) from node-red to mysql and i get some error that is "Error: ER_PARSE_ERROR: You have an error in your SQL syntax; ".i dont understand about datetime data type in mysql. i choose vary type such as varchar,date,datetime but not accomplish. II. second problem is how to send only YYYY-MM-DD time (2020-12-10 21.11.50) to mysql.Colud someone help me please? thank you
regard.
var date=new Date().toString();
msg.topic = "INSERT INTO `test-sequence` VALUES ("+new Date().toString()+","+msg.payload["Vrms"]+","+msg.payload["Irms"]+","+msg.payload["Power"]+","+ msg.payload["Energy"] +","+ msg.payload["PF"] +","+ msg.payload["Frequency"] +")";
return msg;
Even better, if you always want to save the current time then make sure the column is type TIMESTAMP and set it DEFAULT CURRENT_TIMESTAMP. Then you don't need to provide a timestamp at all.
thank you verymuch for answer, when i copy your code i had error like picture below.
i change something it show like this
var date = new Date().toISOString();
msg.topic = "INSERT INTO `test-sequence` VALUES ("+date+","+msg.payload["Vrms"]+","+msg.payload["Irms"]+","+msg.payload["Power"]+","+ msg.payload["Energy"] +","+ msg.payload["PF"] +","+ msg.payload["Frequency"] +")";
return msg;
Feed the function node output into a debug node set to show Complete Message and see what is in msg.topic.
[Edit] You would be much better to use the backtick syntax suggested earlier.,,It is much easier to get right. Do not bother feeding the message into the sql node until the topic coming from the function node is what you expect.
That is an international standard time (ISO format) - it represents UTC and this is how you REALLY should store time in a database. Only when you present it to a user do you convert to local time.
Read up on "why you should store utc in database"
If you want to store local time I would strongly recommend you at least also store UTC time alongside it (you will thank me when timezone changes or daylight savings changes or an international user looks at your data)
i get it now. your word very real because ISO time can convert any timezone and very correct time. I very appreciate you.Thankyou
i will store two time that is ISO time , GMT+7 time. i think the code will be like this