SQLite3; weird error

Discussion in 'Python' started by TYR, Oct 29, 2007.

  1. TYR

    TYR Guest

    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.
     
    TYR, Oct 29, 2007
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Oct 29, 2007
    #2
    1. Advertising

  3. TYR

    Duncan Booth Guest

    TYR <> wrote:

    > 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.
     
    Duncan Booth, Oct 29, 2007
    #3
  4. TYR

    TYR Guest

    On Oct 29, 11:51 am, Duncan Booth <>
    wrote:
    > TYR <> wrote:
    > > 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.


    Ah. Apologies for a silly question.
     
    TYR, Oct 29, 2007
    #4
  5. 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.
     
    Bruno Desthuilliers, Oct 29, 2007
    #5
  6. TYR a écrit :
    > On Oct 29, 11:51 am, Duncan Booth <>
    > wrote:
    >
    >>TYR <> wrote:
    >>
    >>>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.

    >
    >
    > Ah. Apologies for a silly question.
    >

    Not so silly (considering you're somewhat new to Python, which seems
    obvious here) - some languages work otherwise.
     
    Bruno Desthuilliers, Oct 29, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dorayme
    Replies:
    1
    Views:
    657
    richard
    Jan 21, 2011
  2. richard
    Replies:
    0
    Views:
    617
    richard
    Jan 21, 2011
  3. David Vallner

    Weird bug in SQLite3

    David Vallner, Feb 5, 2006, in forum: Ruby
    Replies:
    1
    Views:
    106
    David Vallner
    Feb 6, 2006
  4. Jeffrey 'jf' Lim
    Replies:
    5
    Views:
    540
    Jeffrey 'jf' Lim
    Apr 9, 2007
  5. SunSw0rd
    Replies:
    4
    Views:
    292
    SunSw0rd
    Jul 2, 2009
Loading...

Share This Page