Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 31st, 2004, 03:03 PM
ofricker ofricker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 ofricker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old May 31st, 2004, 03:44 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,254 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 9 m
Reputation Power: 265
Long answer:

From the docs for the db API:
Quote:
.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

Reply With Quote
  #3  
Old June 3rd, 2004, 08:27 AM
ofricker ofricker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 7 ofricker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Cheers
Oli

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Resultset in brackets


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT