informatica:lenguajes_de_programacion:python:python_db_postgresql
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previa | |||
| informatica:lenguajes_de_programacion:python:python_db_postgresql [2025/09/18 07:17] – [Python : psycopg2 : fetchone : ejemplo simple] admin | informatica:lenguajes_de_programacion:python:python_db_postgresql [2026/04/15 02:03] (actual) – [Leer tabla] admin | ||
|---|---|---|---|
| Línea 152: | Línea 152: | ||
| * [[https:// | * [[https:// | ||
| + | |||
| + | ===== Ejemplos psycopg2 ===== | ||
| + | |||
| + | <code python> | ||
| + | |||
| + | # 1. Instalar librerías | ||
| + | !pip install psycopg2-binary sqlalchemy | ||
| + | |||
| + | # 2. Importar librerías | ||
| + | import psycopg2 | ||
| + | import pandas as pd | ||
| + | from sqlalchemy import create_engine | ||
| + | |||
| + | # 3. Configurar parámetros de conexión | ||
| + | host = ' | ||
| + | dbname = ' | ||
| + | user = ' | ||
| + | password = ' | ||
| + | port = ' | ||
| + | |||
| + | # 4. Crear conexión con SQLAlchemy (recomendado para pandas) | ||
| + | engine = create_engine(f' | ||
| + | |||
| + | # 5. Leer datos con pandas | ||
| + | query = " | ||
| + | df = pd.read_sql(query, | ||
| + | print(df) | ||
| + | |||
| + | </ | ||
| + | |||
| Backups Automáticos: | Backups Automáticos: | ||
| * [[https:// | * [[https:// | ||
informatica/lenguajes_de_programacion/python/python_db_postgresql.1758179858.txt.gz · Última modificación: 2025/09/18 07:17 por admin
