[DB-SIG] problem with SELECT

Federico Di Gregorio fog@mixadlive.com
15 Aug 2001 12:17:47 +0200

On 15 Aug 2001 03:11:53 -0700, Ian wrote:
> Hey all,
> i'm trying to write a script that updates a database. I dont seem to have 
> any problems inserting data into the tables.
> But I'm trying to get the ID for a the records and its not working.
> I tried using cursor.fetchone and all i get returned is None, and i also 
> tried using cursor.execute and i just get an empty tuple.
> The sql statement works fine when i go into postgres and run it there.
> I've tried doing 'SELECT NEXTVAL("id_sequence")' and i also tried something 
> simple like 'SELECT * FROM table'

you should use *both* .execute() and .fetchone(). if 'c' is a cursor,
you can then do:

	c.execute('SELECT NEXTVAL("id_sequence")')
	id = c.fetchone()[0]

note that you should wrap the code in a try/except block, to catch
possible errors.

hope this helps,

Federico Di Gregorio
MIXAD LIVE Chief of Research & Technology              fog@mixadlive.com
Debian GNU/Linux Developer & Italian Press Contact        fog@debian.org
              All programmers are optimists. -- Frederick P. Brooks, Jr.