Advertisement
ksieradzinski

Untitled

Jun 6th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. from sys import argv
  2. import psycopg2
  3.  
  4. def init():
  5. connection = psycopg2.connect("dbname=todo user=postgres password=Password!")
  6. cursor = connection.cursor()
  7. cursor.execute("""
  8. CREATE TABLE todos (
  9. id SERIAL PRIMARY KEY,
  10. title TEXT NOT NULL,
  11. created_at TIMESTAMP DEFAULT NOW(),
  12. is_done BOOLEAN DEFAULT FALSE
  13. )
  14. """)
  15. connection.commit()
  16. connection.close()
  17.  
  18.  
  19. class TodoDB:
  20. def __init__(self, dsn:str):
  21. self.dsn = dsn
  22.  
  23. def connect(self):
  24. return psycopg2.connect(self.dsn)
  25.  
  26. def add_todo(self, title):
  27. with self.connect() as conn:
  28. cursor = conn.cursor()
  29. cursor.execute("INSERT INTO todos (title) VALUES(%s)", (title,))
  30. conn.commit()
  31.  
  32. def get_todos(self):
  33. with self.connect() as conn:
  34. cursor = conn.cursor()
  35.  
  36.  
  37. if len(argv) > 1 and argv[1] == "setup":
  38. init()
  39.  
  40. #
  41. # init()
  42. #
  43. # db = TodoDB("dbname=todo user=postgres password=Password!")
  44. # db.add_todo("Idź na spacer")
  45. # db.add_todo("Powieś pranie")
  46. # db.add_todo("Ugotuj obiad")
  47.  
  48.  
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement