3

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:警告:行1の列'created_at'の範囲外の値cursor.execute(* sql1)

どうすればこれを解決できますか?ありがとう。

4

1 に答える 1

3

日付フィールドに長い日付を挿入しているため、警告が表示されます。提出された日付は「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」を渡すか、列を日時に変更してください。

幸運を

于 2012-06-08T20:50:59.520 に答える