简体   繁体   English

MySQL中的python(?)占位符

[英]python (?) placeholders in MySQL

I have a working MySQL query in python using (?) place holders, my problem is when I add a WHERE clause in the query, with an extra placeholder (?) SQL won't allow it, I think because it groups placeholders so won't allow the 6th placeholder in the where clause. 我在python中使用(?)占位符有一个工作的MySQL查询,我的问题是当我在查询中添加一个WHERE子句时,有一个额外的占位符(?)SQL将不允许它,我认为因为它组合占位符所以赢了在where子句中允许第6个占位符。

Here's my code snippet: 这是我的代码片段:

self.query.prepare("UPDATE tbl_dev (user_id, dev_ref,dev_mac,dev_ka_der_file_path,dev_ds_der_file_path,dev_type) VALUES (?,?,?,?,?,?) WHERE id=?;")
self.query.bindValue(0, userID)
self.query.bindValue(1, dev_id)
self.query.bindValue(2, dev_mac)
self.query.bindValue(3, dev_ka_cert)
self.query.bindValue(4, dev_ds_cert)
self.query.bindValue(5, dev_type)
self.query.bindValue(6, devID)

self.query.exec_()

Basically it won't except the final devID placeholder as I think it's grouping the 6th (devID) with the first group of placeholders. 基本上它不会除了最终的devID占位符,因为我认为它将第6个(devID)与第一组占位符分组。 Is this even possible in Python/MySQL? 这在Python / MySQL中甚至可能吗?

There's an incorrect syntax as pointed out by @vkp, the working solution was with a select clause, with an update clause there are no grouped values. 如@vkp所指出的语法不正确,工作解决方案是使用select子句,update子句没有分组值。 Solution is below. 解决方案如下。

self.query.prepare("UPDATE tbl_dev SET user_id=?,dev_ref=?,dev_mac=?,dev_ka_der_file_path=?,dev_ds_der_file_path=?,dev_type=? WHERE id=?;")
self.query.bindValue(0, userID)
self.query.bindValue(1, dev_id)
self.query.bindValue(2, dev_mac)
self.query.bindValue(3, dev_ka_cert)
self.query.bindValue(4, dev_ds_cert)
self.query.bindValue(5, dev_type)
self.query.bindValue(6, devID)

self.query.exec_()

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

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