From 9a771b0782583f43773afed522e47c750777d124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Ke=C3=9Fler?= Date: Thu, 2 Mar 2023 11:52:15 +0100 Subject: [PATCH] start implementation of database framework: create table --- database.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 database.py diff --git a/database.py b/database.py new file mode 100644 index 0000000..e3fa7c2 --- /dev/null +++ b/database.py @@ -0,0 +1,30 @@ +import psycopg2 + +## global connection +conn = psycopg2.connect("dbname=hanab-live user=postgres") + +## cursor +cur = conn.cursor() + +## check if table exists, else create it +cur.execute("SELECT EXISTS (SELECT FROM pg_tables WHERE schemaname = 'public' AND tablename = 'games');") +a = cur.fetchone() + +if a[0] is False: + print("creating table") + cur.execute( + "CREATE TABLE games (" + "id SERIAL PRIMARY KEY," + "num_players SMALLINT NOT NULL," + "seed TEXT NOT NULL," + "score SMALLINT NOT NULL," + "variant_id SMALLINT NOT NULL," + "deck_plays BOOLEAN," + "one_extra_card BOOLEAN," + "one_less_card BOOLEAN," + "all_or_nothing BOOLEAN," + "num_turns SMALLINT" + ");") + conn.commit() +else: + print("table already exists")