CSV数据未使用python导入到mysql数据库

时间:2019-02-08 05:38:17

标签: python-2.7 mysql-workbench mysql-python mysql-connector-python

import pymysql
import pandas as pd
import sys

load_sql= "LOAD DATA LOCAL INFILE 
'C:/Users/ksureshb/Documents/Masterdata1.csv' INTO TABLE as.iris_master 
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n'; "
host="localhost"

def csv_to_mysql(load_sql, host="localhost", user="root", passwd="",database="as"):
    '''
    This function load a csv file to MySQL table according to
    the load_sql statement.
    '''
    print("code started")

    con = pymysql.connect(host="localhost",
                                user="root",
                                password="",
                                autocommit=True,
                                local_infile=1)
    print('Connected to DB: {}'.format(host))
        # Create cursor and execute Load SQL
    cursor = con.cursor()
    cursor.execute(load_sql)
    print('Succuessfully loaded the table from csv.')
    con.close()

enter image description here

代码执行得很好。 CSV文件约有200000行,只有第一行反映在mysql数据库中。任何帮助将不胜感激。

0 个答案:

没有答案