New run() method to simplify things in pg8000

From: Tony Locke <tlocke(at)tlocke(dot)org(dot)uk>
To: pgsql-interfaces(at)lists(dot)postgresql(dot)org
Subject: New run() method to simplify things in pg8000
Date: 2020-04-16 15:43:57
Message-ID: CAPpF0yM58d1DObMZaujYb_dVMZspbzHVKq5__h1-RS4zd7cWww@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

Hi all, pg8000 is a pure-Python driver for Postgres
https://github.com/tlocke/pg8000 and in the latest version of pg8000
I've added a run() method to the connection that lets you run SQL
statements without needing a cursor. The idea is to make things a bit
simpler for people. There are a few other differences too. The run()
method always uses the 'named' parameter style, and the parameters are
always provided as keyword arguments. Here's an example:

>>> import pg8000
>>>
>>> # Connect to the database with user name postgres
>>>
>>> con = pg8000.connect("postgres", password="C.P.Snow")
>>>
>>> # Create a temporary table
>>>
>>> con.run("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)")
()
>>>
>>> # Populate the table
>>>
>>> for title in ("Ender's Game", "The Magus"):
... con.run("INSERT INTO book (title) VALUES (:title)", title=title)
()
()
>>>
>>> # Print all the rows in the table
>>>
>>> for row in con.run("SELECT * FROM book WHERE id > :min_id", min_id=0):
... print(row)
[1, "Ender's Game"]
[2, 'The Magus']
>>>
>>> # Commit the transaction
>>>
>>> con.commit()

I'd be interested in any thoughts / feedback if you have any.

Thanks,

Tony.

Browse pgsql-interfaces by date

  From Date Subject
Next Message sanpi+postgersql 2020-04-27 13:02:38 How to insert default value with libpq?
Previous Message Jonah H. Harris 2020-03-02 15:23:19 Re: MS-ACCESS 2010, ODBC 12.01, Out of Memory