I am trying to insert data from records those are having lists or tuples in the data.I have tried with the following code:
Code:
import mysql.connector
#Create connection, added db we created#
connection = mysql.connector.connect(
host='localhost',
user='root',
password='123',
database='testdb_1'
)
#Create cursor for the connection
my_cursor = connection.cursor()
#Create SQL statement with placeholders and put in variable
mike_placeholders="INSERT INTO users (name,emails,ranksheld) VALUES (%s, %s, %s) "
#Create list (array) of records
records_list = [('Tim',['[email protected]', '[email protected]'],[2,3]), ('Mary',['[email protected]'],[40, 45, 52]), ('Sam',['[email protected]'],None), ('Fred',['[email protected]'],[4]) ]
#Execute cursor, requires SQl statement variable, record variable
my_cursor.executemany(mike_placeholders,records_list)
#Commit the connection to make the change on the database
connection.commit()
When I tried to execute this i got following error:
InterfaceError: Failed executing the operation; Python type list cannot be converted
Can anyone help?
tupleinstead oflist. So,records_listshould be(('Tim',['[email protected]', '[email protected]'],[2,3]), ('Mary',['[email protected]'],[40, 45, 52]), ('Sam',['[email protected]'],None), ('Fred',['[email protected]'],[4]) )emailsandranksheldcolumns where you are passing for each of these lists instead of single values.emailscolumn for example contain multiple emails separated by some separator such as a comma?