def save_vehicle_owner(license_plate, owner_name, owner_contact, owner_address): conn = None cursor = None try: conn = mysql.connector.connect( host=MYSQL_HOST, user=MYSQL_USER, password=MYSQL_PASSWORD, database=MYSQL_DATABASE ) cursor = conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS avbs ( license_plate VARCHAR(255) PRIMARY KEY, owner_name VARCHAR(255) NOT NULL, owner_contact VARCHAR(255), owner_address TEXT, registration_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ) ''') sql = "INSERT INTO avbs (license_plate, owner_name, owner_contact, owner_address) VALUES (%s, %s, %s, %s) ON DUPLICATE KEY UPDATE owner_name=%s, owner_contact=%s, owner_address=%s, registration_timestamp=CURRENT_TIMESTAMP" val = (license_plate, owner_name, owner_contact, owner_address, owner_name, owner_contact, owner_address) cursor.execute(sql, val) conn.commit() logging.info(f"Saved/Updated owner details for license plate: {license_plate}") return True except mysql.connector.Error as err: logging.error(f"MySQL Error saving owner details: {err}") return False finally: if cursor: cursor.close() if conn and conn.is_connected(): conn.close()
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter