Search code examples
mysqlload-data-infile

How to upload timestamp data from a file into mysql


shell> cat /data/mysql/tm.sql
1276609796
1276606325

mysql> CREATE TABLE `tm_table` ( f TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP);
Query OK, 0 rows affected

mysql> LOAD DATA INFILE '/data/mysql/tm.sql' INTO TABLE tm_table;
Query OK, 2 rows affected
Records: 2  Deleted: 0  Skipped: 0  Warnings: 2

mysql> SHOW WARNINGS;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'f' at row 1 |
| Warning | 1265 | Data truncated for column 'f' at row 2 |
+---------+------+----------------------------------------+
2 rows in set

mysql> SELECT f FROM tm_table;
+---------------------+
| f                   |
+---------------------+
| 0000-00-00 00:00:00 |
| 0000-00-00 00:00:00 |
+---------------------+
2 rows in set

I prefer to keep date in the timestamp format. How to upload timestamp data from a file into mysql?


Solution

  • Solution found. Thank the_void for the idea.

    mysql> LOAD DATA INFILE '/data/mysql/tm.sql' INTO TABLE tm_table
        -> (@var1) SET f=FROM_UNIXTIME(@var1);
    Query OK, 2 rows affected
    Records: 2  Deleted: 0  Skipped: 0  Warnings: 0
    
    mysql> select f from tm_table;
    +---------------------+
    | f                   |
    +---------------------+
    | 2010-06-15 16:52:05 |
    | 2010-06-15 17:49:56 |
    +---------------------+
    2 rows in set