SQL 语句中并未使用所有参数

Not all parameters were used in the SQL statement

感谢阅读我的post。

我想做一件简单的事情:从一个数据库中获取信息并注入另一个数据库。

PRAGMA_fl_product = ACCESS_cursor.execute('SELECT Prekes.PrekeID, Prekes.NomNr, AmburiaSandLik.Likutis, Prekes.PardKaina, AmburiaSandLik.Likutis, Prekes.PardKaina FROM Prekes INNER JOIN AmburiaSandLik ON Prekes.PrekeID=AmburiaSandLik.PrekeID;').fetchall()

flproduct="INSERT INTO fl_product (product_id, model, quantity, prices, status) VALUES ('%s', '%s', '%s', '%s', '1') ON DUPLICATE KEY UPDATE quantity='%s', price='%s'"
SQLcursor.executemany(flproduct, PRAGMA_fl_product)

这会引发错误:

SQLcursor.executemany(flproduct, PRAGMA_fl_product)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\mysql\connector\cursor.py", line 606, in executemany
stmt = self._batch_insert(operation, seq_params)
File "C:\Program Files (x86)\Python 3.5\lib\site-packages\mysql\connector\cursor.py", line 553, in _batch_insert
"Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

有人可以帮助我理解我面临的问题吗?

我 SELECT 6 列并尝试使用 6 个“%s”转义条目,但似乎遗漏了一些东西。

你的 INSERT 是错误的。

INSERT INTO fl_product
(product_id, model, quantity, prices)
VALUES
('%s'      , '%s' , '%s'    , '%s'  , 1)
ON DUPLICATE KEY UPDATE quantity='%s', price='%s'"

选择了 5 个值和 4 个列。 1 这个数字是怎么回事?

我发现这个问题是因为我有类似的情况:

curs.execute('INSERT INTO tbl (a, b) VALUES (%s, %s) ON DUPLICATE KEY UPDATE b = %s', (1,2,2))

调用时出现同样的错误。当我将查询更改为命名参数时:

curs.execute('INSERT INTO tbl (a, b) VALUES (%(a)s, %(b)s) ON DUPLICATE KEY UPDATE b = %(b)s', {a: 1, b: 2})

此错误为:

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

由此我得出结论,连接器在更新后无法识别参数。所以我的解决方法是:

curs.execute('INSERT INTO tbl (a, b) VALUES (%(a)s, %(b)s) ON DUPLICATE KEY UPDATE b = VALUES(b)', {a: 1, b: 2})

如果没有重复键错误,它使用 VALUES 函数获取本应写入的新值。参见 mysql docs

在你的情况下,这将是:

flproduct="INSERT INTO fl_product (product_id, model, quantity, prices, status) VALUES ('%s', '%s', '%s', '%s', '1') ON DUPLICATE KEY UPDATE quantity=VALUES(quantity), prices=VALUES(prices)"