0

I have a python script to load data to a mysql table problem that I am running into is following:

Warning: Incorrect integer value: 'user_id' for column 'user_id' at row 1
  +'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',row)
Warning: Invalid TIMESTAMP value in column 'edit_time' at row 1
  +'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',row)

and this is just a one line of many warning lines, I understand the error and based on my understanding I guess the row is still in the string format or the conversion didn't happen properly. I was thinking to cast each element in the row after I read them from tsv file.

example: int(row[0])

but I am not sure how to cast correctly to match MySQL timestamp type for the relevant timestamp element.

#load training data
def readtsv(file_name, con):
    cur = con.cursor()
    with open('file.tsv', 'rb') as f:
        for row in csv.reader(f, delimiter='\t'):
            cur.execute('INSERT INTO mytable(user_id, article_id, revision_id, namespace, edit_time, md5, reverted, reverted_user_id, reverted_revision_id, delta, cur_size)'
                        +'VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',row)
    try:
        con.commit()
    except Exception, e:
        print 'unable to commit'
        print e
4

1 回答 1

0

您是否检查过您的时间戳值是否正确?

http://dev.mysql.com/doc/refman/5.1/en/datetime.html

如果是这样,MySQL 将不会看到错误,因此不会存在警告!

作为提示,只需打印插入语句字符串并尝试通过“PhpMyAdmin”或其他数据库接口工具启动它。

MySQL 的 TIMESTAMP 值的格式为:'YYYYMMDDHHMMSS',因此今天 2012 年 12 月 4 日 21:06:00 将以这种方式写入:'20121204210600' 带引号!

于 2012-12-04T20:08:05.817 回答