[Python] Can I use pymysql.connect() with "with" statement?

Discussão em 'Python' iniciado por Stack, Novembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    The following is listed as example in pymysql:

    conn = pymysql.connect(...)
    with conn.cursor() as cursor:
    cursor.execute(...)
    ...
    conn.close()


    Can I use the following instead, or will this leave a lingering connection? (it executes successfully)

    import pymysql
    with pymysql.connect(...) as cursor:
    cursor.execute('show tables')


    (python 3, latest pymysql)

    Continue reading...

Compartilhe esta Página