Created
March 23, 2021 17:13
-
-
Save Tajcore/57140bd4759b5e1ce87da766318116ce to your computer and use it in GitHub Desktop.
Generate SQL scripts for adding 500000 users in a SQL database
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
from faker import Faker | |
from datetime import datetime | |
import random | |
# Initialize Faker Module | |
random_names = Faker() | |
# Initialize Date | |
date = datetime.now() | |
# Initialize list of random email domains | |
domains = ["yahoo","gmail","outlook","msn"] | |
emails = set() | |
file_ = open("new_setup_db3.sql","a") | |
for _ in range(500000): | |
# Get a random name and split into an array of length 2 | |
name = random_names.name().split(" ") | |
# Assign first name last name and create email using "firstnamelastname@randomdomain.com" | |
first_name_1 = name[0] | |
last_name_1 = name[1] | |
email_1 = first_name_1 + last_name_1 + "@" +random.choice(domains) +".com | |
""" | |
Faker sometimes will send back a name that was sent before and in the database | |
emails are suppose to be unique and with our limited domain names the chances of | |
an email being the same is highly likely, so to prevent that I create a hash set or set | |
that will keep a record of generated emails and while the email is in that set | |
We will keep generating new names until we generate a unique email | |
""" | |
while email_1 in emails: | |
name = random_names.name().split(" ") | |
first_name_1 = name[0] | |
last_name_1 = name[1] | |
email_1 = first_name_1 + last_name_1 + "@" +random.choice(domains) +".com" | |
password_1 = "password123" | |
password_1 = generate_password_hash(password_1, method='pbkdf2:sha256') | |
# We create a string that is our SQL query statement and format it to have our generated user | |
query_user = "INSERT INTO USER(f_name,l_name,e_mail,password,date_created) VALUES('{}','{}','{}','{}','{}');\n".format(first_name_1,last_name_1,email_1,password_1,date) | |
query_profile = "INSERT INTO PROFILE(user_id,profile_pic,dob,biography,gender,location) VALUES(LAST_INSERT_ID(),'user.png','1000-01-01','none','none','none');\n" | |
# Lastly we write the scripts to an sql file and flush the script to our db when we've generated 500k users | |
file_.write(query_user) | |
file_.write(query_profile) | |
# Added to email set | |
emails.append(email_1) | |
print("500000 Users Added") | |
file_.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment