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
David542
  • 104,438
  • 178
  • 489
  • 842

2 Answers2

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.

Community
  • 1
  • 1
martin clayton
  • 76,436
  • 32
  • 213
  • 198
  • 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. – David542 Jan 31 '13 at 23:47
  • Seems like you've got it sorted anyway. I've added a comment on the backticks. – martin clayton Feb 01 '13 at 00:09
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"
David542
  • 104,438
  • 178
  • 489
  • 842