Reputation: 37
I am new to sql and python, and i am currently working on a project to import csv data into mysql using pymysql. The table name in sql is pythontable. The csv file has 5 columns with cusip, permno, permco, issuno, hexcd and its corresponding values. However, my code is not working. please help.
import csv
import pymysql
mydb = pymysql.connect( host = 'ip' , user ='user' , passwd = "pw" , db = "db")
cursor = mydb.cursor()
csv_data = csv.reader('data.csv')
for row in csv_data:
cursor.execute('INSERT INTO pythontable(cusip, permno, permco, issuno, hexcd)' 'VALUES(%s, %s, %s, %s, %s)', row)
mydb.commit()
cursor.close()
Upvotes: 1
Views: 513
Reputation: 142
You should make sure that columns and data type in columns match in MySQL table and CSV file.
You can execute the query as following then:
cursor.execute('INSERT INTO pythontable VALUES(%s, %s, %s, %s, %s)', row)
Upvotes: 1