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.

0

1 Answer 1

7

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

4
  • Did you ask the asker to pay a bounty two write his code for him in the other question, or did I misinterpret it? Commented Aug 1, 2012 at 2:43
  • @TankorSmash: Not a bounty specifically; I also accept PayPal transfers and Amazon Canada gift certificates. Commented Aug 1, 2012 at 2:45
  • Well, I guess everybody needs to make a living somehow. Commented Aug 1, 2012 at 2:53
  • 6
    This answer is obsolete; MySQLdb has had this feature for some time. See my related answer.
    – Air
    Commented Oct 6, 2015 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.