From: Daniele Varrazzo Date: Thu, 27 Mar 2025 11:05:25 +0000 (+0100) Subject: docs: improve basic usage example X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=5efa2b4649e51c142ce26a3ab8f74db74d559609;p=thirdparty%2Fpsycopg.git docs: improve basic usage example Be consistent with return/print, make the second print actually print something (taking the chance to showcase `executemany()`). --- diff --git a/docs/basic/usage.rst b/docs/basic/usage.rst index b4630ef34..a3bb13e7b 100644 --- a/docs/basic/usage.rst +++ b/docs/basic/usage.rst @@ -49,11 +49,17 @@ Here is an interactive session showing some of the basic commands: # Query the database and obtain data as Python objects. cur.execute("SELECT * FROM test") - cur.fetchone() - # will return (1, 100, "abc'def") + print(cur.fetchone()) + # will print (1, 100, "abc'def") + + # You can use `cur.executemany()` to perform an operation in batch + cur.executemany( + "INSERT INTO test (num) values (%s)", + [(33,), (66,), (99,)]) # You can use `cur.fetchmany()`, `cur.fetchall()` to return a list # of several records, or even iterate on the cursor + cur.execute("SELECT id, num FROM test order by num") for record in cur: print(record)