python
import hashlib
def generate_short_code(url):
hash_object = hashlib.md5(url.encode())
short_code = hash_object.hexdigest()[:8]
return short_code
python
import mysql.connector
def store_mapping(short_code, long_url):
connection = mysql.connector.connect(
host="localhost",
user="your_user",
password="your_password",
database="short_links"
)
cursor = connection.cursor()
insert_query = "INSERT INTO mappings (short_code, long_url) VALUES (%s, %s)"
record = (short_code, long_url)
cursor.execute(insert_query, record)
connection.commit()
cursor.close()
connection.close()