I'm trying to insert rows in my sql table using a csv file in python.
There are five columns (id(autoincrement, primary, int),Event(int), Edition(int), Subscription(varchar), Daystogo(varchar)
My csv has 4 columns (Event, Edition, Subscription, Daystogo). I want to insert these into the table and let id be assigned in autoincremental way.
mydb = mysql.connector.connect(
user='user', password='password',
host='host',
database='opm')
mycursor = mydb.cursor()
csv_data=csv.reader("mycsvfile.csv")
for row in csv_data:
print(row)
mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)", (int,int,str, str))
This is the error that i have been receiving
csv_data=csv.reader("mycsvfile.csv")
for row in csv_data:
print(row)
mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)", (int,int,str, str))
['m']
Traceback (most recent call last):
File "<ipython-input-51-5b5e2c804099>", line 4, in <module>
mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)", (int,int,str, str))
File "E:\Data Science\pyWork\PyProjects\Program\lib\site-packages\mysql\connector\cursor.py", line 547, in execute
psub = _ParamSubstitutor(self._process_params(params))
File "E:\Data Science\pyWork\PyProjects\Program\lib\site-packages\mysql\connector\cursor.py", line 430, in _process_params
"Failed processing format-parameters; %s" % err)
ProgrammingError: Failed processing format-parameters; Python 'type' cannot be converted to a MySQL type
for code
mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)")
the error is
ProgrammingError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s,%s,%s,%s)' at line 1
Please help me out how can I insert the rows in mysql table
Assuming your CSV has four columns,
mycursor.execute("INSERT INTO opm (Event, Edition, Subscription, Daystogo) VALUES (%s,%s,%s,%s)", row)
should work (the second parameter needs to be a list or a tuple with elements to be bound to the placeholders; since you have four placeholders, you need a four-element list or tuple of data (not data types !).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.