Re: Opening a DB Connection
Gerhard Häring <[email protected]>
| Newsgroups | gmane.comp.python.db.pysqlite.user |
|---|---|
| Message-ID | <[email protected]> |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Rich Shepard wrote:
> [...]
> Also, is there a man page somewhere of the cursor methods? I see
> .fetchone()[0], but I don't understand the end part. What other methods are
> available for navigating through a set returned by a SELECT statement?
I would have liked to refer you to the Pyhton documentation at
http://docs.python.org/lib/sqlite3-Cursor-Objects.html which is more or
less a manual conversion from the pysqlite docs.
Unfortunately, I only documented the things that are particular to pysqlite
there, and not the things that are common among all DB-API modules.
So the best I know is the database API specification itself at
http://www.python.org/dev/peps/pep-0249/ in the section cursor methods.
In our case, fetchone()[0]:
fetchone() returns one row from the result set as a sequence. fetchone()[0]
thus is the value of the first column of the first row of the result set. I
use this construction for SELECT statements that only return a single value.
The recommended way to iterate through a resultset is to just iterate over
the cursor:
cur.execute("select a, b, c from testtable")
for row in cur:
print row
The other methods that are available are fetchonemany() and fetchall().
fetchmany(n) is used to iterate over the result set in batches of n rows,
and fetchall just returns the whole result set as a list of sequences.
HTH,
- -- Gerhard
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFFHkKPdIO4ozGCH14RAvybAJ0fPttmtoD2OblkdcE/tlxtIhzfRgCbBj/g
/blDODN7ktZ52OW+erhELno=
=7EQX
-----END PGP SIGNATURE-----