r/learnpython • u/MooseNew4887 • 13h ago
Rate my customer/employee id generation logic
def generate_id(connection_tuple, id_user = "customer"):
if id_user.lower() == "customer":
table = "customer"
id_string = "cust"
elif id_user.lower() == "employee":
table = "employees"
id_string = "emp"
cursor = connection_tuple[1]
connection = connection_tuple[0]
try_cursor_execute(f"select count(*) form {table}")
last_person = cursor.fetchone()[0]
current_person = last_person+1
no_of_zeros = 6 - len(str(current_person))
id = id_string + "0"*no_of_zeros + str(current_perosn)
return idI have to build a hotel database system thing for my 12th grade Computer Science project.. This is the customer id / employee id generation logic I came up with. Employee IDs are 9 characters, and customer IDs are 10 characters. But this thing seems sort of janky. Is there a better way to do this?
try_cursor_execute() is a function I defined, it is cursor.execute but in a try block. connection tuple is (connection, cursor)