语法在MySQL上可以使用,但在MYSQL DB Connector (Python)上不能使用。

问题描述 投票:0回答:1

这是我的第一个问题,对不起,如果它是一个有点乱,我执行这个查询MYSQL

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

但在MYSQL DB Connector上却无法使用。然而,如果我改变了 %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()
python mysql sql mysql-python
1个回答
0
投票

请确保你的选择与insert中的列数和类型相匹配。

    "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"
© www.soinside.com 2019 - 2024. All rights reserved.