This commit is contained in:
ada-dmitry
2024-04-28 13:37:53 +03:00
parent 7cd1ab1f2e
commit 308b719963
6 changed files with 21 additions and 19 deletions
Binary file not shown.
-9
View File
@@ -39,14 +39,6 @@ class DbTable:
self.dbconn.conn.commit() self.dbconn.conn.commit()
return return
def delete(self, val):
sql = "DELETE FROM " + self.table_name()
sql += " WHERE " + "".join(self.column_names_without_id())
sql += "=" + "'" + "".join(val) + "';"
cur = self.dbconn.conn.cursor()
cur.execute(sql)
self.dbconn.conn.commit()
def drop(self): def drop(self):
sql = "DROP TABLE IF EXISTS " + self.table_name() sql = "DROP TABLE IF EXISTS " + self.table_name()
cur = self.dbconn.conn.cursor() cur = self.dbconn.conn.cursor()
@@ -66,7 +58,6 @@ class DbTable:
cur = self.dbconn.conn.cursor() cur = self.dbconn.conn.cursor()
try: try:
cur.execute(sql) cur.execute(sql)
self.dbconn.conn.commit()
except psycopg2.errors.UniqueViolation: except psycopg2.errors.UniqueViolation:
self.dbconn.conn.rollback() self.dbconn.conn.rollback()
return return
+2 -2
View File
@@ -138,11 +138,11 @@ class Main:
if self.cath_id == -1: if self.cath_id == -1:
while True: while True:
name = input("Укажите название категории для вывода блюд (0 - отмена):") name = input("Укажите название категории для вывода блюд (0 - отмена):")
while len(name.strip()) == 0: while len(name.strip()) == '':
name = input("Пустая строка. Повторите ввод! Укажите название категории для вывода блюд (0 - отмена):") name = input("Пустая строка. Повторите ввод! Укажите название категории для вывода блюд (0 - отмена):")
if name == "0": if name == "0":
return "1" return "1"
cath = CathTable().find_by_position(int(name)) cath = CathTable().find_by_name(name)
if not cath: if not cath:
print("Введено неверное название!") print("Введено неверное название!")
else: else:
Binary file not shown.
Binary file not shown.
+19 -8
View File
@@ -13,14 +13,25 @@ class CathTable(DbTable):
def table_constraints(self): def table_constraints(self):
return ['CONSTRAINT "Name" UNIQUE (cath_name)'] return ['CONSTRAINT "Name" UNIQUE (cath_name)']
def delete(self, val):
def find_by_position(self, num): sql = "DELETE FROM " + self.table_name()
sql += " WHERE " + "".join(self.column_names_without_id())
sql = "SELECT * FROM " + self.table_name() sql += "=" + "'" + "".join(val) + "';"
sql += " ORDER BY "
sql += ", ".join(self.primary_key())
sql += " LIMIT 1 OFFSET %(offset)s"
cur = self.dbconn.conn.cursor() cur = self.dbconn.conn.cursor()
cur.execute(sql, {"offset": num - 1}) cur.execute(sql)
self.dbconn.conn.commit()
def find_by_name(self, name):
cur = self.dbconn.conn.cursor()
sql_sel = "SELECT id FROM " + self.table_name()
sql_sel += " WHERE cath_name = " + "'" + name + "'" + ";"
cur.execute(sql_sel)
# sql = "SELECT * FROM " + self.table_name()
# sql += " ORDER BY "
# sql += ", ".join(self.primary_key())
# sql += " LIMIT 1 OFFSET %(offset)s"
# cur.execute(sql, {"offset": num - 1})
# print(cur)
return cur.fetchone() return cur.fetchone()