gpt4 book ai didi

Python DELETE mysql/mariadb 与 python 变量不起作用

转载 作者:行者123 更新时间:2023-11-29 16:41:10 26 4
gpt4 key购买 nike

我正在尝试做下面帖子中描述的事情,我用一个基本的Python程序简化了这个过程,而不是使用flask,只是为了证明我的概念,但我遇到了一些问题。

flask_mysqldb Delete FROM variable table

我的代码如下;

import mysql.connector as mariadb

new = 'ksk'
mariadb_connection = mariadb.connect(user='root', password='mypwd',
database='customers')
cursor = mariadb_connection.cursor()

cursor.execute("DELETE FROM customer_info WHERE name = %s" % (new))

mariadb_connection.commit()
print('its gone')

这会在我的表中抛出一个错误,

"unknown column 'ksk' in where clause" 

这不是真的,我可以看到一行名称为 ksk。

为了进一步证明这一点,如果我将删除语句更改为:

cursor.execute("DELETE FROM customer_info WHERE name = 'ksk'

行 ksk 已成功删除。我哪里出错了?

最佳答案

https://pynative.com/python-mysql-execute-parameterized-query-using-prepared-statement/ --

We then added those two columns value in the input tuple in sequential order and passed SQL update query and input tuple t0 cursor.execute() function, remember tuple contain user data in sequential order of placeholders.

The CURSOR’s execute() method takes an optional second argument containing a list of data values. each entry in the list must be in tuple format to associate with parameter markers in the statement.

关于Python DELETE mysql/mariadb 与 python 变量不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53328746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com