MySQL Forums
Forum List  »  Connector/Python

cursor close and db commit
Posted by: jupiter hce
Date: February 09, 2013 02:53AM

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.

Options: ReplyQuote


Subject
Written By
Posted
cursor close and db commit
February 09, 2013 02:53AM
February 13, 2013 02:20AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.