Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

update query python mysql.connector

I’m doing a web scraping project and now I’m trying to store my data into a MySQL database (remote server).
I’m sure the DB config is done write, but I keep getting this error:

Traceback (most recent call last):
  File "C:**\main.py", line 14, in <module>
    cnx.commit()
File "C:**\mysql\connector\connection_cext.py", line 406, in commit
    self._cmysql.commit()
_mysql_connector.MySQLInterfaceError: Commands out of sync; you can't run this command now

Here is my code (main.py):

import mysql.connector

cnx = mysql.connector.connect(user='XXXX', password='XXXXX',
                              host='XXXXXXXX',
                              database='sql4456946')
cursor = cnx.cursor()

maxID = ("SET @lastid = (SELECT MAX(`id`) FROM `stand`); "
         "UPDATE `stand` SET `price` = 9999 WHERE `id` = @lastid")
cursor.execute(maxID)
cnx.commit()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have a multiquery so you must use the parameter in the connection string

But you could make it in one query only

CREATE TABLE stand(id int ,price DECIMAL(19,2) )
INSERT INTO stand VALUES (1,100)
    UPDATE `stand` SET `price` = 9999 WHERE `id` = (SELECT MAX(`id`) FROM (SELECT `id` FROM `stand`)  st)
SELECT * FROM stand
id |   price
-: | ------:
 1 | 9999.00

db<>fiddle here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading