|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Long answer:
From the docs for the db API: Quote:
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 |
|
#3
|
|||
|
|||
|
Cheers
Oli |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Resultset in brackets |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|