【问题标题】:Syntax works on MySQL but doesn't works on MYSQL DB Connector (Python)语法适用于 MySQL,但不适用于 MYSQL DB Connector (Python)
【发布时间】:2020-08-27 02:41:48
【问题描述】:

这是我的第一个问题,如果有点乱,抱歉,我在 MYSQL 上执行了这个查询

INSERT INTO ms_print_stock (product_code, product_name, product_stock, product_unit) SELECT * FROM ms_product WHERE product_code = %s

但它不适用于 MYSQL DB 连接器。但是,如果我将 %s 更改为它可以工作的值。它返回此错误。

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version
for the right syntax to use near '%s' at line 1

顺便说一下,这里是python的完整代码

self.mycursor = self.mydb.cursor()
self.sql_print = "INSERT INTO ms_print_stock (product_code, product_name, product_stock, product_unit) SELECT * FROM ms_product WHERE product_code = %s"
print(self.sql_print)
self.val_print = (self.txtPCode.text())
self.mycursor.execute(self.sql_print, self.val_print)
print("BERHASIL")
self.mydb.commit()

【问题讨论】:

  • 嗨 Clarissa,请参阅此处以了解如何更好地格式化您问题中的代码:meta.stackexchange.com/questions/22186/…
  • 试试self.val_print = (self.txtPCode.text(),)(注意多余的逗号)
  • 这工作得很好!谢谢!我想知道额外的逗号做了什么?

标签: python mysql sql mysql-python


【解决方案1】:

确保您的选择与插入中的列数和类型匹配

    "INSERT INTO ms_print_stock (product_code, product_name, product_stock, product_unit) 
        SELECT col_x_product_code
            , col_x_product_name
            , col_x_product_stock
            , col_x_product_unit
         FROM ms_product WHERE product_code = %s"

【讨论】:

  • 你确定你有一个有效的 %s 内容 self.val_print 值.. (self.txtPCode.text())
  • 是的,我尝试打印它,它返回 product_code 的值。最奇怪的是,我尝试使用相同的值和条件进行查询,它适用于其他查询,但不适用于这个特定的查询。
  • 产品代码的值是多少.. ??给我看看确切的样本
  • self.sql = "更新 ms_product SET product_stock = product_stock + (SELECT (quantity_in - quantity_out) AS total FROM surat_jalan WHERE nomorsj = %s AND product_code = %s) WHERE product_code = %s"这工作得很好
  • 不是同一个查询 .. 如果您使用的是 product_code 并引发错误,请向我展示(如果可以的话)一个清晰的示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 2011-12-24
  • 1970-01-01
  • 2014-08-11
  • 2011-03-12
  • 2012-04-14
相关资源
最近更新 更多