我正在重新设计一个数据库,该数据库当前将格式为ISO 8601的日期2012-10-27T07:30:38+00:00导入到varchar字段中.当前数据库托管在MySQL 5.5服务器上.
在搜索文档和各种SO帖子时,我还没有找到关于我应该在MySQL中使用什么数据类型的明确答案.最接近的帖子是: MySQL插入到DATETIME:使用ISO :: 8601格式是否安全?它提供各种各样的工作,但这不是一个理想的选择.
MySQL文档(http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html)没有说明,我在官方文档中找到的唯一参考位于页面上:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
其中指出"第一个和第二个参数的可能值导致几个可能的格式字符串(对于使用的说明符,请参阅DATE_FORMAT()函数说明中的表格.)ISO格式是指ISO 9075,而不是ISO 8601."
现在,PostgreSQL文档专门提到了ISO8601(http://www.postgresql.org/docs/9.2/static/datetime-keywords.html和http://www.postgresql.org/docs/9.2/static/datatype-datetime. html)这引出了我的问题:
MySQL是否正确支持ISO 8601,还是应该考虑使用本机支持的数据库?
- 编辑 -
尝试将上面的示例时间戳插入到datetime列中会出现以下错误:
10:55:55 insert into test(date1) values('2012-10-27T07:30:38+00:00') Error Code: 1292. Incorrect datetime value: '2012-10-27T07:30:38+00:00' for column 'date1' at row 1 0.047 sec
Run Code Online (Sandbox Code Playgroud) 我试图在'日期'中插入一个时间戳:
INSERT INTO dates VALUES (4, "2011-10-04 12:58:36 -0600")
Run Code Online (Sandbox Code Playgroud)
4只是一个ID.在表中,它插入为:
2011-10-04 12:58:36 or 0000-00-00 00:00:00
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,时差-0600丢失了.我怎么也插入它?
错误日志:
{[错误:日期时间值不正确:'2012-08-24T17:29:11.683Z'对于第1行的'robot _refreshed_at'列号]:1292,sqlStateMarker:'#',sqlState:'22007',消息:'不正确datetime value:\'2012-08-24T17:29:11.683Z \'for column \'robot_refreshed_at \'at row 1',sql:'INSERT INTO users(id,name,count_moments,count_likes,count_followers,rob ot_refreshed_at,robot_count_followers )VALUES(\'1834084 \',\'NNNyingzi \',\'5 \',\'0 \',\'0 \',\'2012-08-24T17:29:11.683Z \',\' 0 \')',setMaxListeners:[Function],emit:[Function],addListener:[Function],on:[Function],once:[Function],removeListener:[Function],removeAllListeners:[Function],listeners: [功能]}
我在我的代码中使用了这段代码 Node.js
if s instanceof Date
return s.toISOString()
Run Code Online (Sandbox Code Playgroud)
并在数据库中更新它们.
所述SQL插入物表达如下:
INSERT INTO users (id,name,count_moments,count_likes,count_followers,rob ot_refreshed_at,robot_count_followers) VALUES (\'1834084\',\'NNNyingzi\',\'5\',\ '0\',\'0\',\'2012-08-24T17:29:11.683Z\',\'0\')
Run Code Online (Sandbox Code Playgroud)
我做错了吗?我只是使用PHPMyAdmin服务器中的表复制了一个表.
非常感谢.
我正在解析具有以下数据结构的xml文件:
<row Id="253858" UserId="40883" Name="Scholar" Date="2009-03-08T01:52:32.570" />
<row Id="253860" UserId="19483" Name="Supporter" Date="2009-03-08T01:57:31.733" />
<row Id="253861" UserId="74951" Name="Autobiographer" Date="2009-03-08T02:02:32.390" />
Run Code Online (Sandbox Code Playgroud)
我使用ruby脚本来解析这些数据并将它们插入到mysql数据库中.以下是我的数据表的样子:
+---------+-------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | |
| user_id | int(11) | NO | | NULL | |
| name | varchar(40) | YES | | NULL | |
| created | timestamp | NO | | CURRENT_TIMESTAMP | on …Run Code Online (Sandbox Code Playgroud)