简体   繁体   English

Python删除带有python变量的mysql / mariadb不起作用

[英]Python DELETE mysql/mariadb with a python variable not working

I am trying to do something as described in the post below, I have simplified this with a basic python program, not using flask, just to prove my concept, but I'm having some issues. 我正在尝试按照以下文章中的描述进行操作,我已使用基本的python程序(不使用flask)简化了此操作,只是为了证明我的概念,但是我遇到了一些问题。

flask_mysqldb Delete FROM variable table flask_mysqldb删除FROM变量表

My code is as follows; 我的代码如下:

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')

this throws up an error saying in my table, 这引发了一个错误,说我的桌子上,

"unknown column 'ksk' in where clause" 

this isn't true I can see a row where the name is ksk. 这不是真的,我可以看到名称为ksk的行。

Furthermore to prove this, if I change my delete statement to: 为了证明这一点,如果我将delete语句更改为:

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

the row ksk gets successfully deleted. 行ksk被成功删除。 Where am I going wrong? 我要去哪里错了?

https://pynative.com/python-mysql-execute-parameterized-query-using-prepared-statement/ -- 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. 然后,我们将这两个列的值按顺序添加到输入元组中,并传递了SQL更新查询和输入元组t0 cursor.execute()函数,请记住,元组按占位符的顺序包含用户数据。

The CURSOR's execute() method takes an optional second argument containing a list of data values. CURSOR的execute()方法采用一个可选的第二个参数 ,该参数包含一个数据值列表。 each entry in the list must be in tuple format to associate with parameter markers in the statement. 列表中的每个条目都必须为元组格式,才能与语句中的参数标记关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM