Created
July 21, 2023 12:47
-
-
Save kriznaraj/c77c0786559cf943a78453c19df48b98 to your computer and use it in GitHub Desktop.
Load dummy payments into payments table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mysql.connector | |
import random | |
import string | |
# MySQL connection settings | |
host = '' | |
user = '' | |
password = '' | |
database = '' | |
# Number of records to insert | |
num_records = 1000000 | |
# Method to generate random string | |
def generate_random_string(length=5): | |
characters = string.ascii_letters + string.digits | |
random_string = ''.join(random.choice(characters) for _ in range(length)) | |
return random_string | |
# Establish a connection to MySQL | |
connection = mysql.connector.connect(host=host, user=user, password=password, database=database) | |
cursor = connection.cursor() | |
# Generate and insert sample records | |
for i in range(num_records): | |
# payment_date = datetime.now() - timedelta(days=random.randint(0, 365)) | |
amount = round(random.uniform(10, 5000), 2) | |
customer_name = generate_random_string() | |
query = "INSERT INTO payments (amount, name) VALUES (%s, %s)" | |
values = (amount, customer_name) | |
cursor.execute(query, values) | |
# Commit the changes and close the connection | |
connection.commit() | |
cursor.close() | |
connection.close() | |
print(f"{num_records} sample records inserted successfully.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment