X-Original-To: pgsql-general-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id A731E5334D for ; Mon, 2 May 2005 07:00:22 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 70444-01 for ; Mon, 2 May 2005 10:00:12 +0000 (GMT) Received: from mail.gmx.net (mail.gmx.net [213.165.64.20]) by svr1.postgresql.org (Postfix) with SMTP id 0AE8753224 for ; Mon, 2 May 2005 07:00:08 -0300 (ADT) Received: (qmail invoked by alias); 02 May 2005 10:00:10 -0000 Received: from p54B987A3.dip0.t-ipconnect.de (EHLO hermes.hilbert.loc) [84.185.135.163] by mail.gmx.net (mp001) with SMTP; 02 May 2005 12:00:10 +0200 X-Authenticated: #1433807 Received: (from ncq@localhost) by hermes.hilbert.loc (8.11.6/8.11.6/SuSE Linux 0.5) id j429u5J01357 for pgsql-general@postgresql.org; Mon, 2 May 2005 11:56:05 +0200 Date: Mon, 2 May 2005 11:56:05 +0200 From: Karsten Hilbert To: pgsql-general@postgresql.org Subject: Python DB-API 2.0 oddity (was: I receieved an example of Rekall script) Message-ID: <20050502115605.B566@hermes.hilbert.loc> Mail-Followup-To: pgsql-general@postgresql.org References: <1a4.372310a7.2fa6dc0b@aol.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1a4.372310a7.2fa6dc0b@aol.com> User-Agent: Mutt/1.3.22.1i X-Y-GMX-Trusted: 0 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests= X-Spam-Level: X-Archive-Number: 200505/28 X-Sequence-Number: 77376 A note on what I think is a strangeness in the Python DB-API 2.0. Please correct me if I am wrong. (Note that I am not trying to throw off the OP but simply use his example to point out an oddity about that API. The point is to make sure it *is* an oddity so I can raise it with the appropriate forum, eg the Python community.) Observe the following notes by someone learning the DB-API: > # Get a "cursor". We do this by connecting to the database: the > # (button,"") arguments just connect to the database that the form is > # running in. > # > cursor = RekallPYDBI.connect (button, '').cursor() So far so good... But - getting a cursor without knowing the SELECT query the results of which it stands for ? > # Execute a query. This only gets people whose ages are 21 or above. > # This is the important bit, we are accessing the database directly. > # > cursor.execute ("select surname from users where age >= ?", [21]) Ah, the query is set *after* getting a cursor for it - seems odd, but hey, as long as it's set before retrieving rows ... > The key is getting the "cursor". Once you have a cursor you can do inserts, > updates and deletes, like Huh ? Pardon me ? Doing inserts, updates and deletes via a cursor ? The PostgreSQL documentation clearly says that the part of a cursor definition must be a SELECT: http://www.postgresql.org/docs/7.4/static/sql-declare.html (I am well aware that SELECT queries may have side effects that change data in the backend such as in "select add_new_customer()" etc.) The sequence of using a cursor in the Python DB-API is as follows: conn = dbapi.connect(...) curs = conn.cursor() curs.execute('[select | insert | update | delete] ...') print curs.fetch(no_of_rows) # if query was a select This seems mighty odd to me. Should it not be: # get connection conn = dbapi.connect(...) # select w/o cursor so we get entire result set right away rows = conn.execute('select ...', use_cursor = False) # select w/ cursor so we can traverse result set as needed curs = conn.execute('select ...'), use_cursor = True) rows = curs.fetch(no_of_rows=25) # insert conn.execute('insert ...') # update conn.execute('update ...') # delete conn.execute('delete ...') Wondering, Karsten -- GPG key ID E4071346 @ wwwkeys.pgp.net E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346