From 75bd352824f03060694a754d028a76d658fa431c Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 13 Nov 2022 19:33:12 -0300 Subject: [PATCH] Add tables Co-authored-by: Ezequiel Bellver Co-authored-by: Juan Barmasch --- backend/main.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/backend/main.py b/backend/main.py index b56ff48..7d0c21e 100644 --- a/backend/main.py +++ b/backend/main.py @@ -47,7 +47,7 @@ def edit_name(id, name): def create_table(name): cur = conn.cursor() - cur.execute(sql.SQL("CREATE TABLE {table} ()").format(table=sql.Identifier(name))) + cur.execute(sql.SQL("CREATE TABLE {table} (row_number SERIAL PRIMARY KEY)").format(table=sql.Identifier(name))) def add_column(name, column, type): @@ -62,20 +62,36 @@ def insert_column(name, column, data): pass -def insert_columns(name, columns, data): - pass +def insert_columns(name, data): + cur = conn.cursor() + + str = "(" + "DEFAULT, %s," * (len(data) - 1) + "%s" + ")" # TODO: change. + # cur.execute(sql.SQL("INSERT INTO {table} VALUES %s").format( + cur.execute(sql.SQL("INSERT INTO {table} VALUES" + str).format( + table=sql.Identifier(name)), + data + ) -def edit_column(name, column, data): - pass +def edit_columns(name, columns, data, id): + cur = conn.cursor() - -def edit_columns(name, columns, data): - pass + i = 0 + for column in columns: + cur.execute(sql.SQL("UPDATE {table} SET {col} = %s WHERE row_number = " + id).format( + table=sql.Identifier(name), + col=sql.Identifier(column)), + [data[i]] + ) + i += 1 def remove_column(name, column): - pass + cur = conn.cursor() + cur.execute(sql.SQL("ALTER TABLE {table} DROP COLUMN {column}").format( + table=sql.Identifier(name), + column=sql.Identifier(column)) + ) def create_sort(name):