Documentation Index
Fetch the complete documentation index at: https://mintlify.com/HotCode2025/Print-Estoy-Cansado-Jefe-TercerSemestre/llms.txt
Use this file to discover all available pages before exploring further.
The four fundamental database operations — Create, Read, Update, Delete (CRUD) — map directly to the SQL statements INSERT, SELECT, UPDATE, and DELETE. psycopg2 lets you run all of them through the same cursor.execute() interface.
A key safety rule: always use parameterized queries (placeholders written as %s) instead of string interpolation. psycopg2 escapes the values for you, which completely prevents SQL injection attacks.
After every INSERT, UPDATE, or DELETE you must call
conexion.commit() (or rely on the with conexion: context manager to do it
automatically). If you skip the commit, the transaction is left open and your
changes will be silently discarded when the connection closes.
SELECT — Reading Records
SELECT queries do not modify data and do not require a commit. Use fetchall() to retrieve multiple rows or fetchone() for a single row. See psycopg2 Basics for a full walkthrough; here is a quick recap:
import psycopg2
conexion = psycopg2.connect(
user='postgres',
password='admin',
host='127.0.0.1',
port='5432',
database='test_bd'
)
try:
with conexion:
with conexion.cursor() as cursor:
sentencia = 'SELECT * FROM persona WHERE id_persona IN (1, 2)'
cursor.execute(sentencia)
registros = cursor.fetchall() # Returns a list of tuples
for registro in registros:
print(registro)
except Exception as e:
print(f'Ocurrió un error: {e}')
finally:
conexion.close()
INSERT — Creating Records
Insert One Record
Pass the SQL template and a tuple of values to cursor.execute(). The with conexion: block automatically commits on success and rolls back on exception:
import psycopg2 # Esto es para poder conectarnos a Postgre
conexion = psycopg2.connect(
user='postgres',
password='admin',
host='127.0.0.1',
port='5432',
database='test_bd'
)
try:
with conexion:
with conexion.cursor() as cursor:
sentencia = "INSERT INTO persona (nombre, apellido, email) VALUES (%s, %s, %s)"
valores = ("Carlos", "Lara", "clara@gmail.com") # tuple of values
cursor.execute(sentencia, valores)
# conexion.commit() — handled automatically by the with block
except Exception as e:
print(f"Ocurrió un error: {e}")
finally:
conexion.close()
Insert Multiple Records
Use cursor.executemany() to insert a collection of rows in a single call. After it finishes, cursor.rowcount tells you how many rows were inserted:
import psycopg2
conexion = psycopg2.connect(
user='postgres',
password='admin',
host='127.0.0.1',
port='5432',
database='test_bd'
)
try:
with conexion:
with conexion.cursor() as cursor:
sentencia = 'INSERT INTO persona (nombre, apellido, email) VALUES (%s, %s, %s)'
valores = (
('Laura', 'Barros', 'Lbarros@mail.com'),
('Marcos', 'Veloz', 'Mveloz@mail.com'),
('Merlin', 'Carracedo', 'Mcarracedo@mail.com'),
)
cursor.executemany(sentencia, valores)
registros_insertados = cursor.rowcount
print(f'Los registros insertados son: {registros_insertados}')
except Exception as e:
print(f'Ocurrió un error: {e}')
finally:
conexion.close()
UPDATE — Modifying Records
Update One Record
Include a WHERE clause with a primary-key placeholder to target a specific row. cursor.rowcount returns the number of affected rows:
import psycopg2 # Esto es para poder conectarnos a PostgreSQL
conexion = psycopg2.connect(
user='postgres',
password='admin',
host='127.0.0.1',
port='5432',
database='test_bd'
)
try:
with conexion:
with conexion.cursor() as cursor:
sentencia = 'UPDATE persona SET nombre=%s, apellido=%s, email=%s WHERE id_persona=%s'
valores = ('Juan Carlos', 'Roldan', 'rcarlos@mail.com', 1) # tuple
cursor.execute(sentencia, valores)
registros_actualizados = cursor.rowcount
print(f'Los registros actualizados son: {registros_actualizados}')
except Exception as e:
print(f'Ocurrió un error: {e}')
finally:
conexion.close()
Update Multiple Records
Pass a tuple of tuples to cursor.executemany() to update several rows at once:
import psycopg2 # Esto es para poder conectarnos a Postgre
conexion = psycopg2.connect(
user='postgres',
password='admin',
host='127.0.0.1',
port='5432',
database='test_bd'
)
try:
with conexion:
with conexion.cursor() as cursor:
sentencia = 'UPDATE persona SET nombre=%s, apellido=%s, email=%s WHERE id_persona=%s'
valores = (
('Juan', 'Perez', 'jperez@mail.com', 4),
('Romina', 'Ayala', 'ayala@mail.com', 5),
) # tuple of tuples
cursor.executemany(sentencia, valores)
registros_actualizados = cursor.rowcount
print(f'Los registros actualizados son: {registros_actualizados}')
except Exception as e:
print(f'Ocurrió un error: {e}')
finally:
conexion.close()
DELETE — Removing Records
Delete One Record
Target a single row with a primary-key placeholder:
import psycopg2 # Esto es para poder conectarnos a Postgre
conexion = psycopg2.connect(
user='postgres',
password='admin',
host='127.0.0.1',
port='5432',
database='test_bd'
)
try:
with conexion:
with conexion.cursor() as cursor:
sentencia = 'DELETE FROM persona WHERE id_persona=%s'
entrada = input('Digite el numero de registro a eliminar: ')
valores = (7,) # tuple of values
cursor.execute(sentencia, valores)
registros_eliminados = cursor.rowcount
print(f'Los registros eliminados son: {registros_eliminados}')
except Exception as e:
print(f'Ocurrió un error: {e}')
finally:
conexion.close()
Delete Multiple Records
Use an IN clause with a tuple of primary keys to remove several rows in one query:
import psycopg2 # Esto es para poder conectarnos a Postgre
conexion = psycopg2.connect(
user='postgres',
password='admin',
host='127.0.0.1',
port='5432',
database='test_bd'
)
try:
with conexion:
with conexion.cursor() as cursor:
sentencia = 'DELETE FROM persona WHERE id_persona IN %s'
entrada = input('Digite los numero de registro a eliminar (separados por coma): ')
valores = (tuple(entrada.split(',')),) # tuple of tuples
cursor.execute(sentencia, valores)
registros_eliminados = cursor.rowcount
print(f'Los registros eliminados son: {registros_eliminados}')
except Exception as e:
print(f'Ocurrió un error: {e}')
finally:
conexion.close()
Quick Reference
| Method | SQL Operation | Rows at a time | Requires commit? |
|---|
cursor.execute() | SELECT / INSERT / UPDATE / DELETE | One statement | No (SELECT) / Yes (writes) |
cursor.executemany() | INSERT / UPDATE / DELETE | Many rows via iteration | Yes |
cursor.fetchone() | SELECT | Returns 1 tuple | No |
cursor.fetchall() | SELECT | Returns all tuples | No |
cursor.rowcount | Any write | — | — (read after execute) |