added updatw cath

This commit is contained in:
ada-dmitry
2024-05-13 13:05:28 +03:00
parent 32a35ea419
commit db5b6c8bdb
7 changed files with 58 additions and 29 deletions
Binary file not shown.
Binary file not shown.
+12 -3
View File
@@ -54,7 +54,6 @@ class DbTable:
for i in range(len(vals)):
if type(vals[i]) == str:
vals[i] = "'" + vals[i] + "'"
pass
else:
vals[i] = str(vals[i])
values = ", ".join(vals)
@@ -89,8 +88,18 @@ class DbTable:
sql += " ORDER BY "
sql += ", ".join(self.primary_key())
cur = self.dbconn.conn.cursor()
cur.execute(sql)
return cur.fetchall()
try:
cur.execute(sql)
return cur.fetchall()
except psycopg2.errors.UndefinedTable:
self.dbconn.conn.rollback()
self.db_drop()
self.db_init()
self.db_insert_somethings()
finally:
cur.execute(sql)
return cur.fetchall()
def select_one(self, **kwargs):
conditions = []
+5 -17
View File
@@ -118,11 +118,11 @@ class Main:
next_step = self.show_dish_in_cath()
elif next_step == "8":
x = int(input(texts.text_choice_update))
if(x==1):
self.cath_update()
elif(x==2):
self.dish_update()
x = int(input('Выберите номер категории для изменения (0 - для отмены): '))
if(x==0):
pass
else:
CathTable().cath_update(self.cath_arr[x-1])
return "1"
elif next_step != "0" and next_step != "9" and next_step != "3":
@@ -151,12 +151,6 @@ class Main:
CathTable().insert_one(data)
return
def update_cath(self):
"""Функция для обновления категории
"""
def show_dish_in_cath(self):
"""Вывод всех блюд в выбранной пользователем категории
"""
@@ -182,17 +176,11 @@ class Main:
print("" + " "*(self.max_index + 1)+ "Название" + " "*(self.max_len_name - 4)\
+"Время приготовления Краткая инструкция\
\n-------------------------------------------------------------------------------------------")
# print('Катя хорошая и Дима тоже')
for i in range(len(self.dish_arr)):
txt = str(i+1) + " "*(2+self.max_index-len(str(i)))
txt += self.dish_arr[i][0] + " "*(4+self.max_len_name - len(self.dish_arr[i][0]))
txt += self.dish_arr[i][1] + " "*(5+19-len(self.dish_arr[i][1]))
txt += self.dish_arr[i][2]
# txt += self.dish_arr[i][0] +
# print(f"""{str(i+1):<8}{self.dish_arr[i][0]:<18}\
# {self.dish_arr[i][1]:<15}{self.dish_arr[i][2]}""")
# print(f"""{str(i+1)}{" "*(self.max_index+2)}\
# {self.dish_arr[i][0]}{" "*(self.max_len_name+2)}{self.dish_arr[i][1]}{" "*19}{self.dish_arr[i][2]}""")
print(txt)
menu = texts.show_dish_in_cath_txt
print(menu)
+39 -7
View File
@@ -21,19 +21,51 @@ class CathTable(DbTable):
self.dbconn.conn.commit()
def find_by_name(self, name):
cur = self.dbconn.conn.cursor()
param_query = "SELECT id FROM cath WHERE cath_name = %s;"
# sql_sel = "SELECT id FROM " + self.table_name()
# sql_sel += " WHERE cath_name = " + "'" + name + "'" + ";"
cur.execute(param_query, (name,))
ret = cur.fetchone()
return list(ret)[0]
# 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)
def cath_update(self, old):
"""Функция для обновления категории
"""
print(old)
cur = self.dbconn.conn.cursor()
data = input("Введите название (1 - отмена): ").strip()
if data == "1":
return
while((len(data.strip()) == 0)or(len(data.strip()) > 32)or(self.check_by_name(data.strip()))):
if (len(data.strip()) > 32):
data = input("Название слишком длинное! Введите название заново (1 - отмена):").strip()
if data == "1":
return
elif len(data.strip()) == 0:
data = input("Название не может быть пустым! Введите название заново (1 - отмена):").strip()
if data == "1":
return
else:
data = input("Такое название уже существует. Введите новое (1 - отмена):").strip()
print(data)
param_sql = f"UPDATE cath SET cath_name = '{data}' WHERE cath_name = '{old}';"
cur.execute(param_sql)
self.dbconn.conn.commit()
def check_by_name(self, value):
sql = f"SELECT * FROM {self.table_name()} WHERE cath_name='{value}'"
cur = self.dbconn.conn.cursor()
cur.execute(sql)
result = cur.fetchone()
cur.close()
if result:
return True
else:
return False
def example_insert(self):
self.insert_one(["Завтрак"])
+1 -1
View File
@@ -10,7 +10,7 @@ show_cath_2txt = """------------------------------------------------------------
3 - добавление новой категории;
4 - удаление категории;
5 - просмотр блюд в категории;
8 - обновить категорию/блюдо;
8 - обновить категорию;
9 - выход."""
show_dish_in_cath_txt = """-------------------------------------------------------------------------------------------\nДальнейшие операции: