473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

sqlite3 question

Hi all,

I am using sqlite3 in python, and I wonder if there is a way to know
if there are valid rows returned or not. For example I have a table
song with one entry in it. The ID of that entry is 1, so when I do;
>>r = c.execute('select * from song where id = 1')
for s in r:
.... print s
....
(1, u'Spikedrivers Blues', u'Mississippi John Hurt')

That works. But when I can't restore the row by e.g. an ID that does
not exist, I cannot see any method in 'r' (which is a SQLite.Cursor)
that can tell me if I have rows. As explained in the help, r.rowcount
does not give a valid result because it can't determine how many rows
are restored in advance.

All I can think of is a 'crappy' construction where I use the iterator
to see if there was something in there, but surely, there must be a
better way to know?
>>r = c.execute('select * from song where id = 2')
notfound = True
for s in r:
.... notfound = False
.... print s
>>if notfound:
.... print 'No rows'

I am pretty new with Python, maybe there are some properties of an
iterator / sqlite3 I am not aware of that can tell me how many rows
are there?

With regards,
- Jorgen
Apr 12 '07 #1
6 3213

Jorgen Bodde wrote:
All I can think of is a 'crappy' construction where I use the iterator
to see if there was something in there, but surely, there must be a
better way to know?
>r = c.execute('select * from song where id = 2')
notfound = True
for s in r:
... notfound = False
... print s
>if notfound:
... print 'No rows'

I am pretty new with Python, maybe there are some properties of an
iterator / sqlite3 I am not aware of that can tell me how many rows
are there?
What about this:

if not c.fetchone():
print "No rows"

or

print "rowcount=", len(cur.fetchall())

--
HTH,
Rob

Apr 12 '07 #2
In <ma***************************************@python. org>, Jorgen Bodde
wrote:
>>>r = c.execute('select * from song where id = 1')
for s in r:
... print s
...
(1, u'Spikedrivers Blues', u'Mississippi John Hurt')

That works. But when I can't restore the row by e.g. an ID that does
not exist, I cannot see any method in 'r' (which is a SQLite.Cursor)
that can tell me if I have rows. As explained in the help, r.rowcount
does not give a valid result because it can't determine how many rows
are restored in advance.
This should not work because `r` should not be a `Cursor` object. The
`execute()`-Method returns an integer with the number of "affected rows".

Ciao,
Marc 'BlackJack' Rintsch
Apr 12 '07 #3
On Thu, 2007-04-12 at 13:43 +0200, Marc 'BlackJack' Rintsch wrote:
In <ma***************************************@python. org>, Jorgen Bodde
wrote:
>>r = c.execute('select * from song where id = 1')
for s in r:
... print s
...
(1, u'Spikedrivers Blues', u'Mississippi John Hurt')

That works. But when I can't restore the row by e.g. an ID that does
not exist, I cannot see any method in 'r' (which is a SQLite.Cursor)
that can tell me if I have rows. As explained in the help, r.rowcount
does not give a valid result because it can't determine how many rows
are restored in advance.

This should not work because `r` should not be a `Cursor` object. The
`execute()`-Method returns an integer with the number of "affected rows".
It does work if 'c' is a connection object with a poorly chosen name.
According to
http://docs.python.org/lib/sqlite3-C...n-Objects.html , sqlite3
connection objects have a non-standard execute method that creates a
cursor, executes a query on that cursor, and returns that cursor.

Anyway, if you expect a query to return at most one row, such as when
you're filtering on the table's primary key, this is how I would do it:

cur.execute("select * from song where id = ?", (wanted_id,) )
song_row = cur.fetchone()
if song_row:
# Do something with song_row
else:
# Song not found

HTH,

Carsten.
Apr 12 '07 #4
Thanks,

This is how I did it in the end as well. Yes i use the connection
object, abbreviated as 'c' for ease of typing.

In my real app the connection is kept inside a singleton object and I
use the DB like

result = GuitarDB().connection.execute('select * from song where id =
1').fetchone()
if result:
print 'Found a song'
else:
print 'Found nothing'

I know there will always be a cursor object back from
connection.execute, so for ease of use and sparing a temp var, I put
the .fetchone() behind the connection.execute()

Thanks everyone for their help!
- Jorgen

On 4/12/07, Carsten Haese <ca*****@uniqsys.comwrote:
On Thu, 2007-04-12 at 13:43 +0200, Marc 'BlackJack' Rintsch wrote:
In <ma***************************************@python. org>, Jorgen Bodde
wrote:
>>>r = c.execute('select * from song where id = 1')
>>>for s in r:
... print s
...
(1, u'Spikedrivers Blues', u'Mississippi John Hurt')
>
That works. But when I can't restore the row by e.g. an ID that does
not exist, I cannot see any method in 'r' (which is a SQLite.Cursor)
that can tell me if I have rows. As explained in the help, r.rowcount
does not give a valid result because it can't determine how many rows
are restored in advance.
This should not work because `r` should not be a `Cursor` object. The
`execute()`-Method returns an integer with the number of "affected rows".

It does work if 'c' is a connection object with a poorly chosen name.
According to
http://docs.python.org/lib/sqlite3-C...n-Objects.html , sqlite3
connection objects have a non-standard execute method that creates a
cursor, executes a query on that cursor, and returns that cursor.

Anyway, if you expect a query to return at most one row, such as when
you're filtering on the table's primary key, this is how I would do it:

cur.execute("select * from song where id = ?", (wanted_id,) )
song_row = cur.fetchone()
if song_row:
# Do something with song_row
else:
# Song not found

HTH,

Carsten.
--
http://mail.python.org/mailman/listinfo/python-list

On 4/12/07, Carsten Haese <ca*****@uniqsys.comwrote:
On Thu, 2007-04-12 at 13:43 +0200, Marc 'BlackJack' Rintsch wrote:
In <ma***************************************@python. org>, Jorgen Bodde
wrote:
>>>r = c.execute('select * from song where id = 1')
>>>for s in r:
... print s
...
(1, u'Spikedrivers Blues', u'Mississippi John Hurt')
>
That works. But when I can't restore the row by e.g. an ID that does
not exist, I cannot see any method in 'r' (which is a SQLite.Cursor)
that can tell me if I have rows. As explained in the help, r.rowcount
does not give a valid result because it can't determine how many rows
are restored in advance.
This should not work because `r` should not be a `Cursor` object. The
`execute()`-Method returns an integer with the number of "affected rows".

It does work if 'c' is a connection object with a poorly chosen name.
According to
http://docs.python.org/lib/sqlite3-C...n-Objects.html , sqlite3
connection objects have a non-standard execute method that creates a
cursor, executes a query on that cursor, and returns that cursor.

Anyway, if you expect a query to return at most one row, such as when
you're filtering on the table's primary key, this is how I would do it:

cur.execute("select * from song where id = ?", (wanted_id,) )
song_row = cur.fetchone()
if song_row:
# Do something with song_row
else:
# Song not found

HTH,

Carsten.
--
http://mail.python.org/mailman/listinfo/python-list
Apr 13 '07 #5
En Thu, 12 Apr 2007 08:43:49 -0300, Marc 'BlackJack' Rintsch
<bj****@gmx.netescribió:
In <ma***************************************@python. org>, Jorgen Bodde
wrote:
>>>>r = c.execute('select * from song where id = 1')
for s in r:
... print s
...
(1, u'Spikedrivers Blues', u'Mississippi John Hurt')
This should not work because `r` should not be a `Cursor` object. The
`execute()`-Method returns an integer with the number of "affected rows".
Actually DBAPI 2.0 says the return value is undefined.

--
Gabriel Genellina
Apr 15 '07 #6
In <op***************@furufufa-ec0e13.cpe.telecentro.com.ar>, Gabriel
Genellina wrote:
En Thu, 12 Apr 2007 08:43:49 -0300, Marc 'BlackJack' Rintsch
<bj****@gmx.netescribió:
>In <ma***************************************@python. org>, Jorgen Bodde
wrote:
>>>>>r = c.execute('select * from song where id = 1')
>for s in r:
... print s
...
(1, u'Spikedrivers Blues', u'Mississippi John Hurt')
>This should not work because `r` should not be a `Cursor` object. The
`execute()`-Method returns an integer with the number of "affected rows".

Actually DBAPI 2.0 says the return value is undefined.
I just remembered the number of affected rows, but that's just for data
manipulation statements like ``UPDATE`` or ``INSERT``. For ``SELECT`` the
method should return `None`. My bad.

Ciao,
Marc 'BlackJack' Rintsch
Apr 16 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lolmcbride | last post by:
Hi, is it possible to pass args through the api which are the same as the args you can use on the sqlite3 command line? What I'm talking about is the .mode or .output commands which you can enter...
2
by: John Machin | last post by:
Apologies in advance if this is a bit bloggy, but I'd like to get comments on whether I've lost the plot (or, more likely, failed to acquire it) before I start reporting bugs etc. From "What's...
66
by: mensanator | last post by:
Probably just me. I've only been using Access and SQL Server for 12 years, so I'm sure my opinions don't count for anything. I was, nevertheless, looking forward to Sqlite3. And now that gmpy...
3
by: cjl | last post by:
P: I am using python 2.5.1 on windows. I have the following code: conn = sqlite3.connect('.\optiondata') c = conn.cursor() try: c.execute('''create table options (ssymbol text, strike real,...
4
by: Simon | last post by:
I installed the source code on unix for python 2.5.1. The install went mainly okay, except for some failures regarding: _ssl, _hashlib, _curses, _curses_panel. No errors regarding sqlite3....
33
by: Stef Mientki | last post by:
hello, I discovered that boolean evaluation in Python is done "fast" (as soon as the condition is ok, the rest of the expression is ignored). Is this standard behavior or is there a compiler...
3
by: milan_sanremo | last post by:
I have sqlite installed, but when I try to import sqlite3 I receive: Python 2.5.1 (r251:54863, Nov 3 2007, 02:54:36) on sunos5 Type "help", "copyright", "credits" or "license" for more...
0
by: Ben Lee | last post by:
hi folks -- a quick python and sqlite3 performance question. i find that inserting a million rows of in-memory data into an in-memory database via a single executemany() is about 30% slower...
15
by: Kurda Yon | last post by:
Hi, I try to "build" and "install" pysqlite? After I type "python setup.py build" I get a lot of error messages? The first error is "src/ connection.h:33:21: error: sqlite3.h: No such file or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.