使用Python语法错误将csv加载到mysql中

时间:2014-07-25 03:55:34

标签: python mysql csv

我有一个TSV文件,其结构如下:

#DAteTime    Userid    tweet (each separated by \t)
2009-06-07 02:07:41 http://twitter.com/hokiepokie728    @fabro84 'Before the Storm' is a new song from the Jonas Brothers that is going to be on their new album. miley has a duet with nick on it!
2009-06-07 02:07:42 'http://twitter.com/annieng'    is in LA now

如何使用python将此文件导入名为tweet的MySQL表?我有以下表结构:

1   DT  datetime
2   USER    varchar(141)    
3   TWEET   varchar(141)    
4   ID  bigint(20)          AUTO_INCREMENT

1 个答案:

答案 0 :(得分:0)

使用mysql-connector-python 1.2.2你可以这样做

import mysql.connector
from mysql.connector.constants import ClientFlag

cnx = mysql.conector.connect(client_flags=[ClientFlag.LOCAL_FILES], ....)

cur = cnx.cursor()
cur.execute("LOAD DATA LOCAL INFILE '</path/to/data/file>' INTO "
            "TABLE tweet")