我一直在阅读关于stackoverflow的其他类似问题,但无法解决此问题。 所有数据库凭证均正确。
import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode
newcat = ["New Arrivals",
"Dresses"]
try:
connection = mysql.connector.connect(user='root', password='root', host='localhost', port='8889', database='legasimall_db')
mySql_insert_query = """INSERT INTO category (categoryname)
VALUES (%s) """
for x in newcat:
recordTuple = (x)
cursor = connection.cursor()
cursor.execute(mySql_insert_query, recordTuple)
connection.commit()
print("Record inserted successfully into Laptop table")
cursor.close()
except mysql.connector.Error as error:
print("Failed to insert record into Laptop table {}".format(error))
finally:
if (connection.is_connected()):
connection.close()
print("MySQL connection is closed")