2

The following works for me:

mysql -u 'root' -h 8.8.8.88 mo -e 'UPDATE `path_last_updated` 
    SET timestamp="2012-01-03 00:00:00"'

However, the following does not:

TIMESTAMP=`date "+%Y-%m-%d-%T"`
mysql -u 'root' -h 8.8.8.88 mo -e 'UPDATE `path_last_updated` 
    SET timestamp=$TIMESTAMP'

How would I insert the timestamp from unix into my mysql table?

Update:

TIMESTAMP=`date "+%Y-%m-%d %T"`
mysql -u 'root' -h 8.8.8.88 mo -e "UPDATE `path_last_updated` 
    SET timestamp='$TIMESTAMP'"

ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual 
that corresponds to your MySQL server version for the right syntax to use near 'SET 
timestamp='2013-01-31 15:46:00'' at line 1
0

2 Answers 2

2

Shell variable interpolation only works between double quotes ("), not single ('). You've also got backticks in there, which in a double-quoted string will be treated as an embedded shell command.

Try:

mysql -u 'root' -h 8.8.8.88 mo -e "UPDATE \`path_last_updated\`
    SET timestamp='$TIMESTAMP'"

Also, fwiw, you have an extra dash (-) in your format for the date command, between the %d and %T.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, thanks. The first half is now working for me -- it's executing with the variable. But it doesn't seem to be a valid SQL statement for whatever reason. Please see updated question.
Seems like you've got it sorted anyway. I've added a comment on the backticks.
0

ALTER TABLE to make it easier:

ALTER TABLE path_last_updated ADD date_entered timestamp DEFAULT CURRENT_TIMESTAMP

And then in shell:

mysql -u 'root' -h 8.8.8.88 mo -e "UPDATE path_last_updated SET timestamp=DEFAULT"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.