Home > database >  How to insert more than two Select Sql inside the same cursor.execute?
How to insert more than two Select Sql inside the same cursor.execute?

Time:10-04

How to insert more than two Select Sql inside the same cursor.execute? I incorrectly write my code. For example in this function:

def example(event=None):
  
    cursor.execute('SELECT aaaaaa From bbbbbb WHERE cccccc = 435',
                   'SELECT aaaaaa From bbbbbb WHERE cccccc = 436',
                   'SELECT aaaaaa From bbbbbb WHERE cccccc = = 437')  

    result=[row[0] for row in cursor]
    example['value'] = result
    example.current(0)
    return result

CodePudding user response:

what's interest of multiplying querys instead of making one query containing the datas your are looking for ?

In your case :

SELECT aaaaaa From bbbbbb WHERE cccccc IN (435,436,437)

i can't see any interest to make more than one query in that case. if you use the query i gave to you, you just have to browse the results one by one with the cursor.

query = "SELECT aaaaaa From bbbbbb WHERE cccccc IN (435,436,437)"
cursor.execute(query)
result=[row[0] for row in cursor]

however if you really want to execute multi statements, you can use the param "multi=True". Here is the doc who explain how to do : https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html

for result in cursor.execute(operation, multi=True):
    ...
  • Related