SQLite3; weird error

Diez B. Roggisch deets at nospam.web.de
Mon Oct 29 07:44:24 EDT 2007


TYR wrote:

> Has anyone else experienced a weird SQLite3 problem?
> 
> Going by the documentation at docs.python.org, the syntax is as
> follows:
> foo = sqlite3.connect(dbname) creates a connection object representing
> the state of dbname and assigns it to variable foo. If dbname doesn't
> exist, a file of that name is created.
> 
> To do anything with it, you then need to create a cursor object by
> calling foo's method cursor (bar = foo.cursor).

is that really from the docs? If yes, they are buggy.

The problem is pretty obvious: foo.cursor is NOT calling a method cursor on
foo. It's a reference to that very method.

So instead write

bar = foo.cursor()

and your done.

> You can now pass an SQL query or command to the DB by calling the
> cursor object's method execute() with the SQL query as a quoted
> statement.
> 
> (bar.execute("SELECT FROM squid WHERE squamous=True")
> 
> And then do other stuff.
> 
> Fine. When I call the cursor object, though, I get an AttributeError;
> ('builtinfunction_or_method object has no attribute 'execute')

Which is a pretty acute description of the error: you are trying to get an
attribute (execute, which you ultimately expect to be a method) from a
method.

Diez



More information about the Python-list mailing list