#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    7
    Rep Power
    0

    Resultset in brackets


    Hi there

    I'm trying to get an integer value out of a db. Instead of the single figure the result is "(7L,)".
    What am I doing wrong? Here's my code:

    Code:
    ...
    conn = MySQLdb.connect(host="localhost", user="root", passwd="password", db="test")
        cursor = conn.cursor()
        cursor.execute("""select id from login
                       where loginname = '%s' and password = '%s' """ % (loginname, pwd))
        result = cursor.fetchall()   
        numrows = int (cursor.rowcount)
        conn.close()
        
        if numrows == 1:
            customerid = result[0]
            print customerid
    Thanks for your help
    Oli
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Long answer:

    From the docs for the db API:
    .fetchall()

    Fetch all (remaining) rows of a query result, returning
    them as a sequence of sequences (e.g. a list of tuples).
    Note that the cursor's arraysize attribute can affect the
    performance of this operation.
    since fetchall() returns a list of tuples, then result[0] is a tuple with one entry for each field in the table returned by the query. In your case there is only one field, but it is still a tuple so you have to index it again to get the field value out of it.

    Short answer:

    replace

    customerid = result[0]

    with

    customerid = result[0][0]


    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    7
    Rep Power
    0
    Cheers
    Oli

IMN logo majestic logo threadwatch logo seochat tools logo