Necessity of explicit cursor.close()

前端 未结 5 1964
既然无缘
既然无缘 2020-12-28 13:57

From time to time, I\'m executing raw queries using connection.cursor() instead of using ORM (since it is definitely not a silver bullet).

I\'ve noticed that in seve

5条回答
  •  遥遥无期
    2020-12-28 14:44

    I'm a bit late to this question. Maybe a close-on-exit-scope is what you want.

    from contextlib import closing
    from django.db import connection
    
    with closing(connection.cursor()) as cursor:
        cursor.execute(...)
        cursor.execute(...)
        cursor.execute(...)
    

提交回复
热议问题