Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

Node.js Updating MySql timestamp

So i do a simple query like so:

connection.query("UPDATE workers SET timestamp='"+thedate+"' WHERE id = " + id,function(err,upres){
    connection.release();
    if(!err) {
      console.log('updated record')
      console.log(upres);
    }

})

console.log reveals the data format as: 2015-04-02 19:29:14

And if i debug the SQL statement, that turns out to be:

UPDATE workers SET timestamp='2015-04-02 21:31:16' WHERE id = 3;

However, when i list the data, the output is:

[{"id":3,"worker":"John Doe","timestamp":"2015-04-01T22:00:00.000Z","duration":30}]

This is way off compared to the time that is being reported?

What is causing this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You do not know how MySQL is turning your VARCHAR into a date. There are a lot of configuration options. It would be better to use the STR_TO_DATE function to circumvent all of the assumptions. Here is a link to the docs for STR_TO_DATE().

As a side note, I would strongly recommend using prepared statements as a way to safeguard your application against errors and sql injection.

EDITS:

In regards to your questions, the column could be DATETIME, but your value you are assigning is a VARCHAR

'UPDATE workers SET timestamp = ? WHERE id = ?', ['4/2/2015 3:00:00 PM', 3'], [callBackFunction]

Based on what you said about the conversion not working, I am suspicious about the data type for the timestamp column.

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE NAME = 'workers'

A statement like that should give you all of the information about that column. You could also find this in a GUI, if you have access. There are three different date types in MySQL date, datetime, or timestamp. This is most likely a DATE column, that will not be able to hold the time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...