Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wait until MySQLdb cursor finish running all queries

Tags:

python

mysql

I am trying to run following code:

with open('file.sql') as sqlFile:
    sql = sqlFile.read()
    cursor.execute(sql)

file.sql contains some SQL queries that initialize a DB.

However this code finishes working before all queries are proceed. They will be proceed in a few seconds after this script finish working.

How can I wait until all queries are proceed?

like image 490
Roman Prykhodchenko Avatar asked Nov 15 '25 18:11

Roman Prykhodchenko


2 Answers

That's already the case.

execute() command already waits for the command to be completely executed before passing to the next line of code in your script.

That means your code won't continue before the cursor.execute() finishes execution.

Your assumptions are wrong. I assume you're getting an error in your sql execution, and that's what causes the sql to be skipped.

For one, cursor.execute() usually can't run multiple SQL statements, so you probably have to split the file into multiple statements and call cursor.execute() multiple times, one for each statement.

Please remove all try/except clauses that might be hiding errors with the code above, and provide the full error traceback you're getting, if that's not the case.

like image 70
nosklo Avatar answered Nov 18 '25 06:11

nosklo


I experienced the same problem. I solved it by call mydb.commit() after mycursor.execute Then, it worked. But I am not sure if it is the lege artis construction.

like image 23
RNA Avatar answered Nov 18 '25 08:11

RNA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!