Zdalne-systemy-z-kit-uVPN/db.py

51 lines
1.3 KiB
Python
Raw Normal View History

2023-04-07 11:25:24 +00:00
import psycopg2
import config
def Connect():
global cur, conn
try:
conn = psycopg2.connect(database=config.database,
host=config.host,
user=config.user,
password=config.password,
port=config.port)
except Exception as ex:
print(f"Error connecting to PostgreSQL: {e}")
cur = conn.cursor()
with conn.cursor() as cur:
cur.execute("""
CREATE TABLE IF NOT EXISTS vpn (
id SERIAL PRIMARY KEY,
image_name VARCHAR(255) NOT NULL,
token VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.commit()
def GetCur():
return conn.cursor()
def GetConn():
return conn
def AddVPNImage(name, token):
Connect()
with GetCur() as cur:
cur.execute("""
INSERT INTO vpn (image_name, token)
VALUES (%s, %s)
""",(name, token,))
conn.commit()
def GetVPNImage(token):
Connect()
with GetCur() as cur:
2023-04-07 11:32:49 +00:00
cur.execute("""
2023-04-07 11:25:24 +00:00
SELECT image_name FROM vpn WHERE token = %s
2023-04-07 11:32:49 +00:00
""",(token,))
return cur.fetchone()
2023-04-07 11:25:24 +00:00