Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sqlite 3 attribute error __exit__

428 views
Skip to first unread message

Sayth Renshaw

unread,
May 15, 2016, 10:21:41 PM5/15/16
to
Hi

I have a file and want to create the sqlite3 db. Using with however i receive an attribute error and it causes an exit.

The relevant section of the file is:

import sqlite3

conn = sqlite3.connect("trial.db")
with conn, conn.cursor() as cur:
# First, create tables.
cur.execute("drop table if exists meetings, races, horses")
cur.execute("create table meetings (" +
", ".join("%s varchar" % fld for fld in meetattrs)
+ ")")
cur.execute("create table races (" +
", ".join("%s varchar" % fld for fld in raceattrs)
+ ")")
cur.execute("create table horses (" +
", ".join("%s varchar" % fld for fld in horseattrs)
+ ")")


However it runs producing this error for line 30 which is
with conn, conn.cursor() as cur:

(pyXML) [sayth@manjaro pyXML]$ python racemeeting.py data/*xml
Traceback (most recent call last):
File "racemeeting.py", line 31, in <module>
with conn, conn.cursor() as cur:
AttributeError: __exit__

Why would i get this error?

Sayth

DFS

unread,
May 15, 2016, 10:45:26 PM5/15/16
to

Ben Finney

unread,
May 15, 2016, 10:53:06 PM5/15/16
to
Sayth Renshaw <flebbe...@gmail.com> writes:

> with conn, conn.cursor() as cur:

What are you expecting this ‘with’ statement to do?

As you've written it, the statement declares your intent to enter both
‘conn’ and ‘conn.cursor()’ as context managers.

<URL:https://docs.python.org/3/reference/compound_stmts.html#with>

To “enter a context manager” entails calling the ‘__enter__’ method on
the context manager object. So that will happen to both ‘conn’ and
‘conn.cursor()’.

Are both of those objects context managers? If not, you will get the
error that you reported.

--
\ “Pinky, are you pondering what I'm pondering?” “I think so, |
`\ Brain, but if we get Sam Spade, we'll never have any puppies.” |
_o__) —_Pinky and The Brain_ |
Ben Finney

Sayth Renshaw

unread,
May 15, 2016, 10:56:45 PM5/15/16
to
cur = conn.cursor() does work thought I should have been able to use with to handle it for me.

Thanks

Sayth

Sayth Renshaw

unread,
May 15, 2016, 11:41:14 PM5/15/16
to
>
> As you've written it, the statement declares your intent to enter both
> ‘conn’ and ‘conn.cursor()’ as context managers.
>
> <URL:https://docs.python.org/3/reference/compound_stmts.html#with>
>
> To “enter a context manager” entails calling the ‘__enter__’ method on
> the context manager object. So that will happen to both ‘conn’ and
> ‘conn.cursor()’.
>
> Are both of those objects context managers? If not, you will get the
> error that you reported.
>

I actually am unsure about context managers. I was talking to ChrisA the other day and using psycog2 this code worked.

conn = psycopg2.connect("")
with conn, conn.cursor() as cur:
# First, create tables.
cur.execute("drop table if exists meetings, races, horses")
cur.execute("create table meetings (" +
", ".join("%s varchar" % fld for fld in meetattrs)
+ ")")
cur.execute("create table races (" +
", ".join("%s varchar" % fld for fld in raceattrs)
+ ")")
cur.execute("create table horses (" +
", ".join("%s varchar" % fld for fld in horseattrs)
+ ")")

I tried to change it to sqlite3 and it fails. I thought it may have been sqlite specific.

Found this http://initd.org/psycopg/articles/2013/04/07/psycopg-25-released/ I found using a context managers search and psycopg2 provides this as a feature.

from the site.

Connections and cursors as context managers

A recent DBAPI extension has standardized the use of connections and cursors as context managers: it is now possible to use an idiom such as:

with psycopg2.connect(DSN) as conn:
with conn.cursor() as curs:
curs.execute(SQL)

with the intuitive behaviour: when the cursor block exits the cursor is closed; when the connection block exits normally the current transaction is committed, if it exits with an exception instead the transaction is rolled back, in either case the connection is ready to be used again (FIXED: the connection is NOT closed as originally stated).

thanks

Sayth



0 new messages