I have written the following code to import CSV data into mysql DB via python. But am getting the following error "MySQLdb._exceptions.ProgrammingError: not enough arguments for format string"
I have tried the following code.
import pandas as pd
import MySQLdb
df = pd.read_csv('D:\\UofG\\orders.csv', sep='|', index_col=False)
print(df)
database = MySQLdb.connect (host="localhost", user="root", passwd="050194.Piku", db = "lineitem")
cursor = database.cursor()
for row in df:
cursor.execute("INSERT INTO ORDER_TABLE (O_ORDERKEY, O_CUSTKEY, O_ORDERSTATUS, O_TOTALPRICE, O_ORDERDATE, O_ORDERPRIORITY, O_CLERK, O_SHIPPRIORITY, O_COMMENT) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", row)
cursor.close()
print ("COMPLETE")
The Error:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\MySQLdb\cursors.py", line 201, in execute
query = query % args
TypeError: not enough arguments for format string
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/NewDB/read_data.py", line 10, in <module>
cursor.execute("INSERT INTO ORDER_TABLE (O_ORDERKEY, O_CUSTKEY, O_ORDERSTATUS, O_TOTALPRICE, O_ORDERDATE, O_ORDERPRIORITY, O_CLERK, O_SHIPPRIORITY, O_COMMENT) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", row)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python37-32\lib\site-packages\MySQLdb\cursors.py", line 203, in execute
raise ProgrammingError(str(m))
MySQLdb._exceptions.ProgrammingError: not enough arguments for format string