From: Billy G. A. <Bil...@mu...> - 2001-11-26 04:09:53
|
markus jais wrote: > hello > > I have the following code: > > ----- > db = PgSQL.connect("::proglang", user="markus", password="markus") > st = db.cursor() > st.execute("select * from languages") > > print st.rowcount # prints -1 > res = st.fetchall() > print st.rowcount # prints 3 > > for i in res: > print i > > st.close > db.close > ---- > > why does the first rowcount print "-1"???? > shouldn't it print "3", as the second, which is the correct answer??? > > the db has three rows: > > the complete output of the programm is: > > -1 > 3 > ['Perl', 'Larry Wall', 'www.perl.com', 'better than C'] > ['Python', 'Guido von Rossum', 'www.python.org', 'very cool'] > ['Ruby', 'Yukihiro Matsumoto', 'www.ruby-lang.org', 'great'] > > > is this a bug?? > or is there something wrong with my understanding of rowcount?? > > markus Markus, That's no bug, that's a feature! :-) By default, pyPgSQL uses PostgreSQL portals (i.e. cursors) when executing a query. This causes the value of rowcount to be indeterminate until a fetchXXX method call is executed. At that time, the rowcount will be set to the number of rows returned by the fetchXXX method call. If you tell pyPgSQL not to use portals, then rowcount will be the number of rows returned by the query. BTW: This behaviour is explained in footnote 7 fo the DB-API 2.0 specification: 7. The rowcount attribute may be coded in a way that updates its value dynamically. This can be useful for databases that return useable rowcount values only after the first call to a .fetchXXX() method. I hope this helps your understanding of rowcount in pyPgSQL. ___________________________________________________________________________ ____ | Billy G. Allie | Domain....: Bil...@mu... | /| | 7436 Hartwell | MSN.......: B_G...@em... |-/-|----- | Dearborn, MI 48126| |/ |LLIE | (313) 582-1540 | |