-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgod.py
More file actions
59 lines (47 loc) · 1.34 KB
/
god.py
File metadata and controls
59 lines (47 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import psycopg2
import urllib.parse as urlparse
from dotenv import load_dotenv
import os
# Cargar variables del archivo .env
load_dotenv()
DATABASE_URL = os.getenv("DATABASE_URL").replace("cockroachdb://", "postgresql://")
url = urlparse.urlparse(DATABASE_URL)
conn = psycopg2.connect(
dbname=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port,
sslmode="verify-full"
)
cur = conn.cursor()
# Crear una tabla si no existe
# cur.execute("""
# CREATE TABLE IF NOT EXISTS productos (
# id SERIAL PRIMARY KEY,
# nombre TEXT NOT NULL,
# precio DECIMAL(10,2) NOT NULL
# );
# """)
# conn.commit()
print("Tabla creada o ya existía.")
cur.close()
conn.close()
# def insertar_producto(nombre, precio):
# conn = psycopg2.connect(
# dbname=url.path[1:],
# user=url.username,
# password=url.password,
# host=url.hostname,
# port=url.port,
# sslmode="verify-full"
# )
# cur = conn.cursor()
# cur.execute("INSERT INTO productos (nombre, precio) VALUES (%s, %s);", (nombre, precio))
# conn.commit()
# print(f"Producto '{nombre}' agregado con éxito.")
# cur.close()
# conn.close()
# # Insertar ejemplos
# insertar_producto("Laptop HP", 2500.99)
# insertar_producto("Mouse Logitech", 150.50)