简体   繁体   中英

Does creating/closing a cursor in “mysql-connector-python” do anything with MySql?

Im using the mysql-connector-python library for a script I wrote to access a MySql 8 Database:

def get_document_by_id(conn,id):
    cursor = conn.cursor(dictionary=True)
    try:
        cursor.execute("SELECT * FROM documents WHERE id = %s",(id,))
        return cursor.fetchone()
    except Exception as e:
        print(str(e))
        return {}
    finally:
        cursor.close()

Since i need to call this function multiple times during a loop, I was wondering about the following:

Does the act of creating/closing a cursor actually interact with my MySql-Database in any way or is it just used as an abstraction in python for grouping together SQL queries?

Thanks a lot for your help.

The doc says that clearly

For a connection obtained from a connection pool, close() does not actually close it but returns it to the pool and makes it available for subsequent connection requests.

You can also refer to Connector/Python Connection Pooling for further information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM