13

I am using mysql connector.Python 1.0.9 downloaded from MySQL site.

I have a sample table here

DROP TABLE IF EXISTS my_table; 
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT UNIQUE,
Shot VARCHAR(4),
sec varchar(5),
lay VARCHAR(15) NOT NULL,
lay_status VARCHAR(15) NOT NULL,
blk VARCHAR(10) NOT NULL,
blk_status VARCHAR(15) NOT NULL,
pri VARCHAR(10) NOT NULL,
pri_status VARCHAR(15) NOT NULL,
ani VARCHAR(10) NOT NULL,
ani_status VARCHAR(15) NOT NULL,
status VARCHAR(5)
);

INSERT INTO my_table VALUES
(1,'SH01','3','1863','yes','1863','yes','P4645','yes','P4557','yes','Over'),
(2,'SH02','2.5','1863','yes','P4645','no','P4557','yes','1863','no','Over'),
(3,'SH03','0.5','P4645','yes','P4557','yes','1863','yes','1863','yes','WIP'),
(4,'SH04','1.25','1863','no','P4645','no','P4557','yes','1863','yes','RTK'),
(5,'SH05','1','1863','yes','1863','yes','P4645','yes','P4557','yes','WIP'),
(6,'SH06','6','P4557','yes','P4645','yes','P4645','yes','P4557','yes','WIP');

i am able to execute a single SQL statment as below.

import mysql.connector

cnx = mysql.connector.connect(user='scott', database='test')
cursor = cnx.cursor()

SQL = '''
        update my_table 
        set 
        LAY = 'P6682'
        , BLK = 'P6682'
        , ANI = 'P6682'
        where
        Shot = 'SH01';
      '''

cursor.execute(SQL)

and everything is fine and database gets updated correctly.

now when i am trying to update the database with multiple statements as below

import mysql.connector

cnx = mysql.connector.connect(user='scott', database='test')
cursor = cnx.cursor()

SQL = '''
    update my_table 
    set 
    LAY = 'P6682'
    , BLK = 'P6682'
    , ANI = 'P6682'
    where
    Shot = 'SH01';

    update my_table 
    set 
    LAY = '1863'
    , BLK = '1863'
    , ANI = '1863'
    where
    Shot = 'SH02'
'''

cursor.execute(SQL)
cnx.commit()
cur.close()
cnx.close()
cnx.disconnect()

I get below trackback error

Traceback (most recent call last):
  File "Test_Module.py", line 24, in 
  File "C:\Python26\Lib\site-packages\mysql\connector\cursor.py", line 396, in execute
    "Use multi=True when executing multiple statements")
InterfaceError: Use multi=True when executing multiple statements

I update my command as below

cursor.execute(SQL,multi = True)

cnx.commit()
cur.close()
cnx.close()
cnx.disconnect()

Now i dont get any error/Traceback. But the database is not getting updated.

Can any one tell me where am i doing wrong.

4 Answers 4

32

At-last after a long research on docs and help. I could able to solve the issue.

Using a for loop at cursor.execute with multi=True worked. I don't know why we need to loop through.

for result in cursor.execute(SQL, multi=True):
    pass

Without loop just cursor.execute(SQL, multi=True) did not do any changes in the database.

import mysql.connector

cnx = mysql.connector.connect(user='scott', database='test')
cursor = cnx.cursor()

SQL = '''
    update my_table 
    set 
    LAY = 'P6682'
    , BLK = 'P6682'
    , ANI = 'P6682'
    where
    Shot = 'SH01';

    update my_table 
    set 
    LAY = '1863'
    , BLK = '1863'
    , ANI = '1863'
    where
    Shot = 'SH02'
'''

for result in cursor.execute(SQL, multi=True):
    pass

cnx.commit()
cur.close()
cnx.close()
cnx.disconnect()
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I spent hours trying to figure out why this wasn't working.
It'd be great if a mysql.connector guru could explain why the for loop is necessary.
good to know: For python 3.7+, make sure you have MySQL Connector/Python 8.0.13 or later ( bugs.mysql.com/bug.php?id=87818 )
3

Looking at the MySQL docs

If multi is set to True, execute() is able to execute multiple statements specified in the operation string. It returns an iterator that enables processing the result of each statement. However, using parameters does not work well in this case, and it is usually a good idea to execute each statement on its own.

so setting multi=True returns an iterator and if you just want to loop through each statement, the other solution offered works well:

for result in cursor.execute(SQL, multi=True):
    pass

Comments

0

You need to have somekind of commit

cnx.commit()

MySQL reference

1 Comment

,i have cnx.commit() i did not write in my question. i have updated my question with cnx.commit()
-1

Another option is to use executemany, as follows:

sql=('insert into articulos (descripcion, precio) values(%s,%s)')  
data=[('naranjas',22), ("pomelos", 29), ("frutillas", 130)]

cursor.executemany(sql,data) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.