What if I don't close the database connection in Python SQLite

后端 未结 6 2082
慢半拍i
慢半拍i 2020-11-30 00:55

I am doing something like this...

conn = sqlite3.connect(db_filename)

with conn:
    cur = conn.cursor()
    cur.execute( ... )

with

6条回答
  •  [愿得一人]
    2020-11-30 01:26

    For managing a connection to a database I usually do this,

    # query method belonging to a DB manager class
    
    def query (self, sql):
        con = sqlite3.connect(self.dbName)
        with con:
            cur = con.cursor()
            cur.execute(sql)
            res = cur.fetchall()
        if con:
            con.close()
    
        return res
    

    doing so, I'm sure that the connection is explicitly closed.

提交回复
热议问题