Do I need to close cursor every time after mysql conn execution?

get a conn,

multiple commands need to be executed,

do I need to close cursor after executing one sql at a time?

Mar.29,2021

is generally given to the database connection pool to operate the opening and closing of each connection instance
if you get it yourself, it is best to close it at the end of the method execution.


can execute more than one at a time. Use python to raise a chestnut

import MySQLdb as mdb
import sys

conn = mdb.connect(host = 'localhost', user = 'root', passwd = '123456', db = 'test')
 
 cur = conn.cursor()
cur.execute("insert into contact values('key1', 'value1')")
cur.execute("select * from test")
row_num = int(cur.rowcount)
for i in range(row_num):
     row = cur.fetchone()
     print row
 -sharpcommit
 conn.commit()
 cur = conn.cursor()
 conn.close()

this problem is related to the transaction support of MySQL's storage engine. There are many types of storage engines in MySQL, such as: MyISAM, InnoDB and so on. MyISAM does not support transactions, while InnoDB is a transactional database that supports transactions. For example, the InnoDB engine, so the operation of the database data will be done in the pre-allocated cache, only after the commit, the database data will be changed.

Menu