Quantcast
Channel: MySQL Forums - Connector/Python
Viewing all articles
Browse latest Browse all 384

cursor close and db commit (1 reply)

$
0
0
Hi,

I am using MySQLdb and InnoDB, appreciate anyone help to clarify following questitions:

(1) When I run select sql, I use cursor.execute(sql) without db.commit(), I only use db.commit() for insert and update. Someone said to me that I should use db.commit() in select as well, is it necessary?

(2) Which of following action is a good practice, (a) or (b)?
(a) Open db connect, run cursor execute(), close cursor and close the db.
(b) Open db connect for one WEB session connection to run all cursor execute,
and only close the db when a session is closed.

(3) Should cursor.close() be called before db.commit(), or it does not matter?

(3) For following multiple sql executions, should the cursor close for each execution or when all executions are finished? Which of following statements is correct or better practice (a) or (b), or it does not matter?

(a) Close cursor for each sql execution:

sql = "select ...."
cursor.execute(sql)
rows = cursor.fetchall()
cursor.close()

sql = "insert ..."
cursor.execute(sql)
cursor.close()
db.commit()

sql = "select ..."
cursor.execute(sql)
rows = cursor.fetchall()
cursor.close()

db.close()

(b) Close cursor at last:

sql = "select ...."
cursor.execute(sql)
rows = cursor.fetchall()

sql = "insert ..."
cursor.execute(sql)
db.commit()

sql = "select ..."
cursor.execute(sql)
rows = cursor.fetchall()

cursor.close()
db.close()

(4) In above example, should cursor.close() be called before db.commit(), or it does not matter?

Thank you.

Kind regards.

Viewing all articles
Browse latest Browse all 384

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>