“警告:列的值超出范围”在MySQL中插入日期时间值

2024-10-05 10:44:55 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Python和Mysqldb库从MongoDB中提取一些数据并将其插入MySQL数据库。

+-------------------+------------+------+-----+---------+----------------+
| Field             | Type       | Null | Key | Default | Extra          |
+-------------------+------------+------+-----+---------+----------------+
| id                | int(16)    | NO   | PRI | NULL    | auto_increment |
| subject           | tinytext   | NO   |     | NULL    |                |
| created_at        | datetime   | NO   |     | NULL    |                |
+-------------------+------------+------+-----+---------+----------------+

created_at  = "2012/06/08 11:47:40 -0700"
sql1 = ("INSERT INTO `items` (`description`, `created_at`) VALUES (%s, %s)", (description, created_at)
try:
    cursor.execute(*sql1)
except MySQLdb.Error, e:
    print "Error %s: %s" % (e.args[0], e.args[1])

对于第一次插入数据库,我得到一个警告,但是对于随后的插入,我没有收到这个警告。

conf/db_tools-import.py:53: Warning: Out of range value for column 'created_at' at row 1 cursor.execute(*sql1)

我该怎么解决? 谢谢。


Tags: 数据no数据库警告executemongodbargserror
1条回答
网友
1楼 · 发布于 2024-10-05 10:44:55

您收到一个警告,因为您正在将长日期插入到日期字段中。日期字段是“YYYY-MM-DD”(http://dev.mysql.com/doc/refman/5.1/en/datetime.html)。

我在Mysql中做了自己的测试,看看:

mysql> create table bigtable (created_at date);
Query OK, 0 rows affected (0.07 sec)

mysql> 

Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+-------------------------------------------------+
| Level   | Code | Message                                         |
+---------+------+-------------------------------------------------+
| Warning | 1265 | Data truncated for column 'created_at' at row 1 |
+---------+------+-------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from bigtable;
+------------+
| created_at |
+------------+
| 2012-06-08 |
+------------+
1 row in set (0.00 sec)

如果要避免警告,只需传递“YYYY-MM-DD”或将列更改为datetime

祝你好运

相关问题 更多 >

    热门问题