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.
Next, db object is used to create a cursor object, which in turn is used to execute SQL queries.
connect() supports the following arguments: host , user , password , database , port , unix_socket , client_flags , ssl_ca , ssl_cert , ssl_key , ssl_verify_cert , compress .
In Python mysqldb I could declare a cursor as a dictionary cursor like this: cursor = db.cursor(MySQLdb.cursors.DictCursor) This would enable me to reference columns in the cursor loop by name like this: for row in cursor: # Using the cursor as iterator city = row["city"] state = row["state"]
MySQLdb does not support the context manager protocol. Roll your own, or use oursql instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With