PEP 249 - Enables development with an interface that conforms to the Python Database API Specification v2.0. Refer to the OSS Psycopg documentation (https://www.psycopg.org/psycopg3/docs/) for details.
Here is an example of an application using a package for the Python language (psycopg).
import psycopg
# Connect to an existing database
with psycopg.connect("host=localhost port=27500 dbname=test user=postgres") as conn:
# Open a cursor to perform database operations
with conn.cursor() as cur:
# Execute a command: this creates a new table
cur.execute("""
CREATE TABLE IF NOT EXISTS test (
id serial PRIMARY KEY,
num integer,
data text)
""")
# Pass data to fill a query placeholders and let Psycopg perform
# the correct conversion (no SQL injections!)
cur.execute(
"INSERT INTO test (num, data) VALUES (%s, %s)",
(100, "abc'def"))
# Query the database and obtain data as Python objects.
cur.execute("SELECT * FROM test")
# You can use `cur.fetchmany()`, `cur.fetchall()` to return a list
# of several records, or even iterate on the cursor
for record in cur:
print(record)
# Make the changes to the database persistent
conn.commit()
However, if you are using the Python language package (psycopg), there are the following differences to the OSS Psycopg.