import pyodbc from datetime import datetime, timedelta SERVER = '' DATABASE = '' USERNAME = '' PASSWORD = '' PORT='1433' connection_string = f'DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={SERVER};PORT={PORT};DATABASE={DATABASE};UID={USERNAME};PWD={PASSWORD};TrustServerCertificate=yes' db_connection = pyodbc.connect(connection_string) cursor = db_connection.cursor() #Set default values sign_in_date_time_check = datetime.fromtimestamp(0) card_code_check = False while True: #Get from card when inputed - card id will be a string card_code_input = input() date_time_now = datetime.now() time_now = date_time_now.time() #Find user in the database used only for checking that the user exists cursor.execute('SELECT SignInTime FROM EmployeeRegister WHERE CardID = ?', card_code_input) db_user_entry = cursor.fetchall() db_sign_in_time = db_user_entry[0][0] #if no user info is returned and thus user does not exist reset loop if not db_user_entry: print('User does not exist') continue #Prevent double tap of card if card_code_input == card_code_check and sign_in_date_time_check > date_time_now: print('Double tap prevented') continue #Save sign in/sign out card code and time + 5 seconds sign_in_date_time_check = date_time_now + timedelta(seconds = 50) card_code_check = card_code_input #If user is signed in sign them out if db_sign_in_time: print('Signed Out') cursor.execute('UPDATE EmployeeRegister SET SignInTime = ? WHERE CardID = ?', None, card_code_input) db_connection.commit() continue print('Database updated') #Update database with sign in time cursor.execute('UPDATE EmployeeRegister SET SignInTime = ? WHERE CardID = ?', time_now, card_code_input) db_connection.commit()