Python SQL executemany 语句不起作用
Python SQL executemany statement doesn't work
我正在尝试执行一个删除语句来检查 table 是否有数据帧的 SKU 列中存在的任何 SKU。如果是,它会删除该行。
supplier_name = input("Enter supplier name of the supplier you are updating: ")
df = pd.read_csv("update.csv",sep=',')
cursor = mydb.cursor()
column = df["SKU"]
print(column)
query="""DELETE FROM price_calculations1(Supplier_Name, SKU) VALUES(?,?)"""
cursor.executemany(query,(supplier_name, column))
mydb.commit()
cursor.close()
将代码更改为;
cursor = mydb.cursor()
column = df["SKU"]
print(column)
query="""DELETE FROM price_calculations1 WHERE Supplier_Name=? AND SKU=?"""
cursor.executemany(query,(supplier_name, column))
mydb.commit()
cursor.close()
它给我以下错误:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(Supplier_Name, SKU) VALUES(?,?)' at line 1
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? AND SKU=?' at line 1
这不是 delete 语句的正确语法。
更多细节可以参考文档:https://dev.mysql.com/doc/refman/8.0/en/delete.html
正确的语法是:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [[AS] tbl_alias]
[PARTITION (partition_name [, partition_name] ...)]
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
在您的情况下,您的陈述应如下所示:
DELETE FROM price_calculations1
WHERE Supplier_Name=%s
AND SKU=%s
要使用参数化语句(也称为准备语句),您必须按如下方式创建游标:
cursor = mydb.cursor(prepared=True)
另请注意,executemany
需要可迭代对象的可迭代对象(或“序列或参数”),而 supplier_name
只是一个字符串,因此您也必须更改它。
文档中的 executemany 示例:
data = [
('Jane', date(2005, 2, 12)),
('Joe', date(2006, 5, 23)),
('John', date(2010, 10, 3)),
]
stmt = "INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)"
cursor.executemany(stmt, data)
来源:https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-executemany.html
我正在尝试执行一个删除语句来检查 table 是否有数据帧的 SKU 列中存在的任何 SKU。如果是,它会删除该行。
supplier_name = input("Enter supplier name of the supplier you are updating: ")
df = pd.read_csv("update.csv",sep=',')
cursor = mydb.cursor()
column = df["SKU"]
print(column)
query="""DELETE FROM price_calculations1(Supplier_Name, SKU) VALUES(?,?)"""
cursor.executemany(query,(supplier_name, column))
mydb.commit()
cursor.close()
将代码更改为;
cursor = mydb.cursor()
column = df["SKU"]
print(column)
query="""DELETE FROM price_calculations1 WHERE Supplier_Name=? AND SKU=?"""
cursor.executemany(query,(supplier_name, column))
mydb.commit()
cursor.close()
它给我以下错误:
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(Supplier_Name, SKU) VALUES(?,?)' at line 1 mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? AND SKU=?' at line 1
这不是 delete 语句的正确语法。
更多细节可以参考文档:https://dev.mysql.com/doc/refman/8.0/en/delete.html
正确的语法是:
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [[AS] tbl_alias]
[PARTITION (partition_name [, partition_name] ...)]
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
在您的情况下,您的陈述应如下所示:
DELETE FROM price_calculations1
WHERE Supplier_Name=%s
AND SKU=%s
要使用参数化语句(也称为准备语句),您必须按如下方式创建游标:
cursor = mydb.cursor(prepared=True)
另请注意,executemany
需要可迭代对象的可迭代对象(或“序列或参数”),而 supplier_name
只是一个字符串,因此您也必须更改它。
文档中的 executemany 示例:
data = [
('Jane', date(2005, 2, 12)),
('Joe', date(2006, 5, 23)),
('John', date(2010, 10, 3)),
]
stmt = "INSERT INTO employees (first_name, hire_date) VALUES (%s, %s)"
cursor.executemany(stmt, data)
来源:https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-executemany.html