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

标签 python mysql mariadb

我正在尝试做下面帖子中描述的事情,我用一个基本的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/

相关文章:

mysql - 从开始日期到结束日期每周显示计划程序事件

python - SymPy 中矩阵的逆矩阵?

python - 选择字段填充错误

python - 如何从 PDF Python 中检测颜色

php 表单提交后返回页面

MySQL - 使用 WHERE 子句进行内部连接

mysql - 将表从一个 MariaDB 数据库克隆到另一个数据库,包括默认值、索引和触发器?

php - mysqli_fetch_array($link, $query) 直接查询数据库时未输出预期结果

python - 从集合中迭代和删除——可能与否?

php - 在 Select 语句中使用子查询