Python array to mysql DB

123 views Asked by At

Why i can't save a array list to mysql DB ^^.

This Script work Code work:

########################################

# Importing modules
import mysql.connector

conn = mysql.connector.connect(
  host="localhost",
  user="*******",
  password="*********",
  database="meineTestDB",
)

cursor = conn.cursor()

insert_stmt = (
   "INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME)"
   "VALUES (%s, %s)"
)
data = ('Test1', 'Test2')

try:
   # Executing the SQL command
   cursor.execute(insert_stmt, data)
   
   # Commit your changes in the database
   conn.commit()

except:
   # Rolling back in case of error
   conn.rollback()

print("Data inserted")

 #Closing the connection
conn.close()
########################################

but i want save array to MySQL DB I try | data = (cars1, cars2) | or | data = ((cars1), (cars2)) | but it doesn't work.

########################################

# Importing modules
import mysql.connector

conn = mysql.connector.connect(
  host="localhost",
  user="*******",
  password="*********",
  database="meineTestDB",
)

cars1 = ["Ford", "Volvo", "BMW"]
cars2 = ["Ford", "Volvo", "BMW"] 

cursor = conn.cursor()

insert_stmt = (
   "INSERT INTO EMPLOYEE (FIRST_NAME, LAST_NAME)"
   "VALUES (%s, %s)"
)
data = (cars1, cars2)

try:
   # Executing the SQL command
   cursor.execute(insert_stmt, data)
   
   # Commit your changes in the database
   conn.commit()

except:
   # Rolling back in case of error
   conn.rollback()

print("Data inserted")

 #Closing the connection
conn.close()
########################################

sry for this Symple question i'm new to that Space. I hope you can help my. THX for all answer. It looks like your post is mostly code; please add some more details. 0.o

0

There are 0 answers