SQLite3; weird error

T

TYR

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).

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')

Am running Python 2.5.1 on Mandriva Linux '08.
 
D

Diez B. Roggisch

TYR said:
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
 
D

Duncan Booth

TYR said:
To do anything with it, you then need to create a cursor object by
calling foo's method cursor (bar = foo.cursor).

Perhaps this would work better if you actually try calling foo's method?

bar = foo.cursor()

Without the parentheses all you are doing is assigning the method to a
variable, not calling it.
 
T

TYR

Perhaps this would work better if you actually try calling foo's method?

bar = foo.cursor()

Without the parentheses all you are doing is assigning the method to a
variable, not calling it.

Ah. Apologies for a silly question.
 
B

Bruno Desthuilliers

TYR a écrit :
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).

You actually want :

bar = foo.cursor()

In Python, the parens are not optional when it comes to calling
functions - in fact, parens *are* the call operator. Without them, you
don't call the function (or method), but retrieve a reference to it.
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')

Indeed. cf above.
 
B

Bruno Desthuilliers

TYR a écrit :
Ah. Apologies for a silly question.
Not so silly (considering you're somewhat new to Python, which seems
obvious here) - some languages work otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top