Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import argv
- import psycopg2
- def init():
- connection = psycopg2.connect("dbname=todo user=postgres password=Password!")
- cursor = connection.cursor()
- cursor.execute("""
- CREATE TABLE todos (
- id SERIAL PRIMARY KEY,
- title TEXT NOT NULL,
- created_at TIMESTAMP DEFAULT NOW(),
- is_done BOOLEAN DEFAULT FALSE
- )
- """)
- connection.commit()
- connection.close()
- class TodoDB:
- def __init__(self, dsn:str):
- self.dsn = dsn
- def connect(self):
- return psycopg2.connect(self.dsn)
- def add_todo(self, title):
- with self.connect() as conn:
- cursor = conn.cursor()
- cursor.execute("INSERT INTO todos (title) VALUES(%s)", (title,))
- conn.commit()
- def get_todos(self):
- with self.connect() as conn:
- cursor = conn.cursor()
- if len(argv) > 1 and argv[1] == "setup":
- init()
- #
- # init()
- #
- # db = TodoDB("dbname=todo user=postgres password=Password!")
- # db.add_todo("Idź na spacer")
- # db.add_todo("Powieś pranie")
- # db.add_todo("Ugotuj obiad")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement