I want to insert a pandas dataframe into a MySQL table using the code below.
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="pw",
database='db')
cols = "`,`".join([str(i) for i in df.columns.tolist()])
c = mydb.cursor(buffered=True)
for i,row in df.iterrows():
sql = "INSERT INTO `table` (`" +cols + "`) VALUES (" + "%s,"*(len(row)-1) + "%s)"
c.execute(sql, tuple(row)) # I've also tried (tuple(row,)) with the same result
mydb.commit()
When I run this I get the following:
---------------------------------------------------------------------------
MySQLInterfaceError Traceback (most recent call last)
<ipython-input-195-e2067aefba0c> in <module>
11 sql = "INSERT INTO `bas` (`" +cols + "`) VALUES (" + "%s,"*(len(row)-1) + "%s)"
12 print('row:',tuple(row))
---> 13 c.execute(str(sql), tuple(row))
14
15 # the connection is not autocommitted by default, so we must commit to save our changes
c:\programdata\miniconda3\lib\site-packages\mysql\connector\cursor_cext.py in execute(self, operation, params, multi)
255
256 if params:
--> 257 prepared = self._cnx.prepare_for_mysql(params)
258 if isinstance(prepared, dict):
259 for key, value in prepared.items():
c:\programdata\miniconda3\lib\site-packages\mysql\connector\connection_cext.py in prepare_for_mysql(self, params)
663 ]
664 else:
--> 665 result = self._cmysql.convert_to_mysql(*params)
666 elif isinstance(params, dict):
667 result = {}
MySQLInterfaceError: Python type list cannot be converted
I find this a bit confusing since I don't insert any list into c.execute() - just a str (sql) and a tuple(row). This isn't a reproducible example, but if anyone has any clue what I could do to solve this I would be grateful.
print(row)before executing the cursor: could it be that one of the values withinrowis a list?.to_sqlmethod? It handles inserting a pandas df into an SQL database. pandas.pydata.org/docs/reference/api/…