By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,499 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

NoneType object returned by .fetchone() in MySQLdb

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Arnaud-F. FAUSSE wrote:
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
Jul 18 '05 #2

P: n/a
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

"Peter Otten" <__*******@web.de> a écrit dans le message de
news:bq*************@news.t-online.com...
Arnaud-F. FAUSSE wrote:
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

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.