Dies ist eine alte Version des Dokuments!
Es gibt mehrere Möglichkeiten das commit oder rollback über Context-Blöcke zu steuern
with psycopg.connect(someconnectionninformation) as connection:
cursor = connection.cursor()
cursor.execute(somestatement)
cursor.execute(someotherstatement)
Außerdem wird die Verbindung zur Datenbank danach geschlossen
with psycopg.connect(someconnectionninformation) as connection:
cursor = connection.cursor()
with connection.transaction():
cursor.execute(somestament)
cursor.execute(somestament)
with connection.transaction():
cursor.execute(somestament)
cursor.execute(somestament)
Man muss allerdings aufpassen, wenn außerhalb der transaction-Blöcke Statements ausgeführt werden, dann starten diese implizit Transaktionen und die transaction-Blöcke erstellen dann nur savepoint sub-transactions.
Ggf. sollte man beim connect dann autocommit=True setzen, dann werden alle Statements außerhalb der transaction-Blöcke als eigene Transaktionen gehandhabt, die sofort commited sind.
conn.execute(insert into whatever values(%s); select * from somethingelse where id = %s, ("firstvalue", "secondvalue"))
create temp table somename (somerow varchar) on commit delete rows;
Daten in eine Tabelle einfügen:
with cursor.copy("copy table_name (column1, column2) from stdin") as copy:
for record in records:
copy.write_row(record)
Daten aus einer Tabelle auslesen:
with cursor.copy("copy table_name (column1, column2) to stdout") as copy:
for record in copy.rows():
Some action