PYTHON:mysql连接器插入失败

时间:2019-10-30 13:45:51

标签: python mysql-connector

我一直在阅读关于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")

0 个答案:

没有答案