【问题标题】:Prepared Statements in MySQL - Trying to remove a row results in ProgrammingErrorMySQL中的Prepared Statements - 尝试删除一行导致ProgrammingError
【发布时间】:2020-08-26 06:10:09
【问题描述】:

我想使用准备好的语句从表中删除一行,但是会导致错误:mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

相关代码:

db = mysql.connector.connect(
    host='localhost',
    user='user',
    database='web_board',
    password='password',
    auth_plugin='mysql_native_password'
)

crs = db.cursor()

# construct the query and remove post from the database
query = 'DELETE FROM posts WHERE postid=%s'
crs.execute(query, tuple(request.data))

db.commit()

crs.close()
db.close()

request.data 看起来像这样:b'9b23f24e-ff4d-4113-85ae-ff8a4a5de3be'

【问题讨论】:

    标签: python mysql python-3.x prepared-statement mysql-python


    【解决方案1】:

    作为documentation states,要使用准备好的语句,您应该使用以下配置实例化您的光标:

    crs = db.cursor(prepared=True)
    

    使用 MySQLCursorPrepared 执行的预处理语句可以使用格式 (%s) 或 qmark (?) 参数化样式。

    【讨论】:

      猜你喜欢
      • 2011-05-05
      • 2014-03-29
      • 1970-01-01
      • 2012-10-04
      • 2011-07-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 2013-08-25
      相关资源
      最近更新 更多