RE: unsubscriptable object error

Discussion in 'Python' started by Tim Golden, Aug 18, 2005.

  1. Tim Golden

    Tim Golden Guest

    []

    > getting an unsubscriptable object error on an if else statement


    > db = MSSQL.connect(server,login,pwd,database)
    > c = db.cursor()
    > c.execute(mySQL)
    > rows = c.fetchone()


    > #determine recipient
    > if str(rows[3]) == str(rows[4]):
    > recipient = str(rows[0]) + " " + str(rows[1])
    > else:
    > recipient = "test"


    > the above code works fine if rows[3] == rows[4]. it's when it doesn't equal that it gives the error::


    > TypeError: unsubscriptable object
    > args = ('unsubscriptable object',)


    Hi, Matthew.

    Couple of points first: if it's possible, try to send emails in plain-text
    only, no HTML or Rich-Text. Makes life easier in some viewers. (And this
    list is gatewayed to news:comp.lang.python and to Google Groups).

    Also, try to screen dump the *actual* traceback, not your
    copy of it. Your example is self-contained so I imagine it
    can easily be run at the interpreter. (Which may be what
    you're doing).

    Finally, I suspect that your (slightly ill-named) "rows" is getting
    a None back from c.fetchone (). Consider the following example:

    <code>
    import MSSQL

    # substitute your own connection info
    db = MSSQL.connect ("VODEV1", "", "", "EVODEV")

    c = db.cursor ()
    c.execute ("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE 1 = 2")
    # guaranteed no rows
    rows = c.fetchone ()

    print rows
    # will display 'None'

    print rows[3], rows[4]
    # will display unsubscriptable object
    </code>

    I realise that you say "the above code works find if
    rows[3] == rows[4]" but how do you know? If I've got the
    wrong end of the stick, can you publish a working snippet
    which can reproduce the error?

    TJG

    ________________________________________________________________________
    This e-mail has been scanned for all viruses by Star. The
    service is powered by MessageLabs. For more information on a proactive
    anti-virus service working around the clock, around the globe, visit:
    http://www.star.net.uk
    ________________________________________________________________________
     
    Tim Golden, Aug 18, 2005
    #1
    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. Balaji
    Replies:
    2
    Views:
    706
    Mel Wilson
    May 22, 2004
  2. Luis P. Mendes

    Pyrex: TypeError: unsubscriptable object

    Luis P. Mendes, May 24, 2005, in forum: Python
    Replies:
    0
    Views:
    1,191
    Luis P. Mendes
    May 24, 2005
  3. Fredrik Lundh

    Re: TypeError: unsubscriptable object

    Fredrik Lundh, Nov 29, 2005, in forum: Python
    Replies:
    0
    Views:
    801
    Fredrik Lundh
    Nov 29, 2005
  4. Replies:
    9
    Views:
    31,255
    Diez B. Roggisch
    Jun 11, 2006
  5. ssmith579

    Unsubscriptable Object Help

    ssmith579, Mar 18, 2009, in forum: Python
    Replies:
    0
    Views:
    3,905
    ssmith579
    Mar 18, 2009
Loading...

Share This Page