Created
March 13, 2019 23:15
-
-
Save hasanparasteh/b47d7feba6ea93d2297a5bd34c46a465 to your computer and use it in GitHub Desktop.
it will genrate random data into 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
""" | |
Usage: first u need to create a database in phpmyadmin and enter | |
ur username and password in Login Information section! | |
After u just need to run the following commands: | |
$ pip install mysql-connector | |
$ pip install names | |
All Done! | |
""" | |
import mysql.connector | |
import names | |
from random import randint | |
from time import sleep | |
# Login Information | |
mydb = mysql.connector.connect( | |
host="localhost", | |
user="root", | |
passwd="", | |
database="RandomPeople" | |
) | |
# Create Table | |
try: | |
mycursor = mydb.cursor() | |
mycursor.execute("CREATE TABLE users (id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, fname VARCHAR(150) NOT NULL, lname VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, birth date NOT NULL)") | |
except: | |
print("Table is already exists") | |
people = int(input("How many names do u want? ")) | |
provider = input("Enter email provider: ") | |
# Insert Data | |
for person in range(people): | |
fname = names.get_first_name() | |
lname = names.get_last_name() | |
email = str(fname + lname + "@" + provider).lower() | |
year = randint(1940,2000) | |
month = randint(1,12) | |
day = randint(1,31) | |
#Month Correction | |
# Febury | |
if(month==2 and day==31): | |
day -= 3 | |
elif(month==2 and day==30): | |
day -= 2 | |
elif(month==2 and day==29): | |
day -= 1 | |
# April | |
if(month==4 and day==31): | |
day -= 1 | |
# June | |
if(month==6 and day==31): | |
day -= 1 | |
# September | |
if(month==9 and day==31): | |
day -= 1 | |
# November | |
if(month==11 and day==31): | |
day -= 1 | |
birth = str(year) + "-"+str(month)+"-"+str(day) | |
# Insert Data | |
sql = "INSERT INTO users (fname, lname, email, birth) VALUES (%s, %s, %s, %s)" | |
val = (fname, lname, email, birth) | |
mycursor.execute(sql, val) | |
mydb.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment