[prev in list] [next in list] [prev in thread] [next in thread]
List: python-db-sig
Subject: [DB-SIG] Context manager support for cursors
From: Gerald Venzl <gerald.venzl () oracle ! com>
Date: 2018-06-20 23:31:42
Message-ID: EDD21F10-AA15-45D8-A42E-958959AEE140 () oracle ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
Hi all,
I was wondering whether there has been any particular reason that the specification \
of the Cursor object doesn't implement a context manager to enable for example the \
"with" statement? Being able to use the "with" statement for database cursors, just \
like other external resources like files, etc., I think makes a lot of sense:
with conn.cursor() as c:
c.execute("SELECT 'test' from dual")
result = c.fetchall()
print(result)
Currently this results in an AttributeError:
File "/Users/gvenzl/test/test.py", line 23, in test
with conn.cursor() as c:
AttributeError: __exit__
Thx,
---------------
Gerald Venzl | Senior Principal Product Manager
Email: gerald.venzl@oracle.com <mailto:gerald.venzl@oracle.com> | Phone: \
+1.650.633.0085 <tel:+16506330085> Oracle ST & Database Development
400 Oracle Parkway | Redwood Shores | 94065 | USA
[Attachment #5 (unknown)]
<html><head><meta http-equiv="Content-Type" content="text/html; \
charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; \
line-break: after-white-space;" class="">Hi all,<div class=""><br class=""></div><div \
class="">I was wondering whether there has been any particular reason that the \
specification of the Cursor object doesn't implement a context manager to enable for \
example the "with" statement?</div><div class="">Being able to use the "with" \
statement for database cursors, just like other external resources like files, etc., \
I think makes a lot of sense:</div><div class=""><br class=""></div><div \
class=""><pre class="" style="font-size: 9pt; background-color: rgb(43, 43, 43); \
color: rgb(169, 183, 198); font-family: Menlo;"><span class="" style="color: rgb(204, \
120, 50);">with </span>conn.cursor() <span class="" style="color: rgb(204, 120, \
50);">as </span>c:<br class=""> c.execute(<span class="" style="color: rgb(106, \
135, 89);">"SELECT 'test' from dual"</span>)<br class=""> result = c.fetchall()<br \
class=""> <span class="" style="color: rgb(136, 136, \
198);">print</span>(result)<br class=""></pre><div class="">Currently this results in \
an AttributeError:</div><div class=""><br class=""></div><div class=""><div \
class=""><font face="Courier New" class=""> \
File "/Users/gvenzl/test/test.py", line 23, in test</font></div><div \
class=""><font face="Courier New" class=""> with conn.cursor() as \
c:</font></div><div class=""><font face="Courier New" class="">AttributeError: \
__exit__</font></div><div class=""><font face="Courier New" class=""><br \
class=""></font></div><div class="">Thx,</div></div></div><div class=""><div \
class=""> <div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; \
text-align: start; text-indent: 0px; text-transform: none; white-space: normal; \
widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: \
break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" \
class=""><div style="color: rgb(0, 0, 0); letter-spacing: normal; orphans: auto; \
text-align: start; text-indent: 0px; text-transform: none; white-space: normal; \
widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: \
break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" \
class=""><div class=""><span lang="EN-GB" style="font-size: 10pt; font-family: \
Verdana, sans-serif; color: rgb(102, 102, 102);" \
class="">---------------</span></div><div class=""><span lang="EN-GB" \
style="font-size: 10pt; font-family: Verdana, sans-serif; color: rgb(102, 102, 102);" \
class=""><br class=""></span></div><div class=""><span lang="EN-GB" style="font-size: \
10pt; font-family: Verdana, sans-serif; color: rgb(102, 102, 102);" class="">Gerald \
Venzl | Senior Principal Product Manager<br class="">Email:<span \
class="Apple-converted-space"> </span></span><span lang="EN-GB" \
style="font-size: 10pt; font-family: Verdana, sans-serif; color: rgb(31, 73, 125);" \
class=""><a moz-do-not-send="true" href="mailto:gerald.venzl@oracle.com" \
class=""><span style="color: black;" \
class="">gerald.venzl@oracle.com</span></a></span><span lang="EN-GB" \
style="font-size: 10pt; font-family: Verdana, sans-serif; color: rgb(102, 102, 102);" \
class=""><span class="Apple-converted-space"> </span>| Phone:<span \
class="Apple-converted-space"> </span><a moz-do-not-send="true" \
href="tel:+16506330085" class=""><span style="color: black;" \
class="">+1.650.633.0085</span></a><br class=""></span><span lang="EN-GB" \
style="font-size: 10pt; font-family: Verdana, sans-serif; color: red;" \
class="">Oracle</span><span lang="EN-GB" style="font-size: 10pt; font-family: \
Verdana, sans-serif; color: rgb(102, 102, 102);" class=""><span \
class="Apple-converted-space"> </span>ST & Database Development<span \
class="Apple-converted-space"> </span><br class=""></span><span lang="EN-GB" \
style="font-size: 10pt; font-family: Verdana, sans-serif;" class=""><span \
style="color: rgb(102, 102, 102);" class="">400 Oracle Parkway | Redwood Shores | \
94065<span class="Apple-converted-space"> </span></span></span><span \
lang="EN-GB" style="font-size: 10pt; font-family: Verdana, sans-serif;" \
class=""><span style="color: rgb(102, 102, 102);" class="">| \
USA</span></span></div></div></div> </div>
<br class=""></div></body></html>
_______________________________________________
DB-SIG maillist - DB-SIG@python.org
https://mail.python.org/mailman/listinfo/db-sig
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic