NoneType object returned by .fetchone() in MySQLdb

A

Arnaud-F. FAUSSE

Hello,

I wrote code to store and fetch information from MySQL, and I have this
problem:
- using .fetchall(), I get tuples (correct according to specification)
- using .fetchone(), I get NoneType object, that I have many problems to use
:-(
Did I miss somthing ?

Any clue ?

Thanks

Arnaud

My configuration:
Windows 2000 SP4
Python 2.3.2
MySQL 4.0.16
MySQLbd 0.9.2
ODBC 3.51.06
 
P

Peter Otten

Arnaud-F. FAUSSE said:
I wrote code to store and fetch information from MySQL, and I have this
problem:
- using .fetchall(), I get tuples (correct according to specification)
- using .fetchone(), I get NoneType object, that I have many problems to
use
:-(
Did I miss somthing ?

Any clue ?

Are you testing with code like below?

cs = conn.cursor()
cs.execute("select * from mytable;")
cs.fetchall()
cs.fetchone()

fetchall() moves the cursor after the last row. When you invoke fetchall()
again it will return an empty list whereas fetchone() will return None.
To fix it, just execute the cursor before fetchone(), e. g:

cs.execute("select * from mytable;")
while 1:
row = cs.fetchone()
if row is None: break
# process row

#this might also work (it does for sqlite)
cs.execute("select * from mytable;")
for row in cs:
# process row

Of course I'm just guessing. It could also be an sql select that returns no
rows or something entirely different. Please post a minimal example code
next time you have a problem.

Peter
 
A

Arnaud-F. FAUSSE

Peter,

my code looks similar to
cs = conn.cursor()
cs.execute("select * from mytable;")
cs.fetchone()
but I don't have a cursor problem due to reaching the end of the selected
data.
The object returned is not None but something like that: (125L,), I expect a
tuple of 1 element containing a long integer. In fact the type of this
object is NoneType and not Tuple as expected.
If I do the same with "fetchall", the returned object has a Tuple type.
I will do some captures and send in a next mail.
Regards
Arnaud
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top