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.
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.