9

I wonder if I can use the context manager with with the Connection object, and write code like this:

with MySQLdb.connect(...) as conn:
    do_something()

Will the conn object be closed automatically after the block like with a file object ?

Thanks.

Derrick Zhang
  • 21,201
  • 18
  • 53
  • 73

1 Answers1

7

MySQLdb does not support the context manager protocol. Roll your own, or use oursql instead.

Community
  • 1
  • 1
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358