1

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()

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.