Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.36 KB | None | 0 0
  1. import sqlite3
  2. from sqlite3 import Error
  3. import random
  4.  
  5. def create_connection(db_file):
  6.     conn = None
  7.     try:
  8.         conn = sqlite3.connect(db_file)
  9.         return conn
  10.     except Error as e:
  11.         print(e)
  12.  
  13.     return conn
  14.  
  15. def create_table(conn, create_table_sql):
  16.     try:
  17.         c = conn.cursor();
  18.         c.execute(create_table_sql)
  19.     except Error as e:
  20.         print(e)
  21.  
  22. def string_generator(lastNameList, firstNameList, titleList, addressList, cityList, i):
  23.     baseCommand = ("""INSERT INTO employees (lastName, firstName, title, reportsTo, address, city, country, phone, email)
  24.    VALUES (""")
  25.    
  26.     command = baseCommand
  27.     tempLastName = random.choice(lastNameList)  #adds last name
  28.     command+= tempLastName
  29.     command+= ", "
  30.     tempFirstName = random.choice(firstNameList)#adds first name
  31.     command+= tempFirstName
  32.     command+= ", "
  33.     command+= random.choice(titleList)  #adds title
  34.     command+= ", "
  35.     command+= str(random.randint(0, i)) #reportsTo can only refer to an ID already on the table, so it runs from 0 to i-1, i being the number of currently inserted employees
  36.     command+= ", "
  37.     command+= random.choice(addressList)#adds a random address
  38.     tempNum = str(random.randint(0, 100))
  39.     tempNum+= "', "
  40.     command+= tempNum   #adds a number from 0 to 99 into the address
  41.     command+= random.choice(cityList) #adds a random city
  42.     command+= ", 'Finland', " #country is always Finland
  43.        
  44.     phoneNum = "'04"
  45.     phoneNum += str(random.randint(0, 9)) #adds an extra digit to the 04 in the beginning of all phone numbers
  46.     phoneNum += " "
  47.     phoneNum += str(random.randint(100, 999)) #adds a 3 digit number
  48.     phoneNum += " "
  49.     phoneNum += str(random.randint(1000, 9999))#adds a 4 digit number
  50.     command+= phoneNum
  51.     command+= "', "
  52.        
  53.     tempLastName = tempLastName.lower()     #for the email, makes last name all lowercase
  54.     tempFirstName = tempFirstName.lower()   #for the email, makes the first name all lowercase
  55.     tempLastName = tempLastName.translate({39: None})   #removes the apostrophes from the last name
  56.     tempFirstName = tempFirstName.translate({39: None}) #removes the apostrophes from the first name
  57.     tempEmail = "'"
  58.     tempEmail+= tempFirstName
  59.     tempEmail+= '.'
  60.     tempEmail+= tempLastName
  61.     tempEmail+= "@company.fi')" #email = 'firstname.lastname@company.fi'
  62.     command+= tempEmail
  63.    
  64.     return command
  65.  
  66. def main():
  67.     database = ("week7.db")
  68.    
  69.     #all lists already have the apostrophes, this way SQLite knows they are TEXT
  70.  
  71.     lastNameList = ["'Jones'", "'Smith'", "'Davis'", "'Miller'", "'Moore'", "'Wilson'", "'Jackson'", "'White'", "'Harris'", "'Garcia'", "'Lee'", "'Walker'", "'Thompson'", "'Lopez'", "'Campbell'", "'Collins'", "'Green'", "'Hill'", "'Reed'", "'Bell'", "'Gray'", "'Cox'", "'Brooks'", "'Ward'", "'Sanders'", "'Wood'", "'Long'", "'Myers'", "'Ford'", "'West'", "'Reynolds'", "'Owens'", "'Cruz'", "'Hunter'", "'Freeman'", "'Palmer'", "'Aalto'", "'Couri'", "'Virtanen'", "'Halko'", "'Kauppi'", "'Jarvinen'", "'Laakso'", "'Kotila'", "'Kivi'"]
  72.    
  73.     firstNameList = ["'Alex'", "'James'", "'John'", "'Robert'", "'Thomas'", "'Daniel'", "'Linda'", "'Mary'", "'Elizabeth'", "'Sarah'", "'Lisa'", "'Betty'", "'Ashley'", "'Carol'", "'Edward'", "'Mark'", "'Ryan'", "'Eric'", "'Laura'", "'Rebecca'", "'Helen'", "'Amy'", "'Ronald'", "'Justin'", "'Nicole'", "'Anna'", "'Emma'", "'Lucas'", "'Jerry'", "'Jose'", "'Joyce'", "'Julia'", "'Maria'", "'Adam'", "'Kyle'", "'Zachary'", "'Olivia'", "'Ethan'", "'Alan'", "'Dylan'", "'Grace'", "'Denise'", "'Amber'", "'Rose'", "'Diana'", "'Natalie'", "'Judy'", "'Morgan'", "'Antti'", "'Samu'", "'Jaakko'", "'Joona'", "'Frans'", "'Zack'", "'Miika'", "'Emilia'", "'Anni'", "'Veera'", "'Oona'", "'Inka'"]
  74.    
  75.     titleList = ["'Manager'", "'Supervisor'", "'Employee'", "'Employee'", "'Employee'", "'Employee'", "'Employee'", "'Employee'", "'Employee'", "'Employee'"]
  76.    
  77.     addressList = ["'Ruskonlahdenkatu ", "'Karankokatu ", "'Keskiortentie ", "'Hakulintie ", "'Lonnrotinkatu ", "'Kaarrostie ", "'Kaarikatu ", "'Puutarhakatu ", "'Harjukuja ", "'Koskikatu ", "'Rauhankatu ", "'Liisankatu ", "'Ysitie ", "'Rauhankatu ", "'Rengaskuja "]
  78.    
  79.     cityList = ["'Lappeenranta'", "'Helsinki'", "'Tampere'", "'Vantaa'", "'Oulu'", "'Turku'", "'Heinola'", "'Rovaniemi'"]
  80.  
  81.     sql_create_table = ("""CREATE TABLE 'employees'(
  82.   [employeeId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  83.   [lastName] NVARCHAR(20)  NOT NULL,
  84.   [firstName] NVARCHAR(20)  NOT NULL,
  85.   [title] NVARCHAR(30),
  86.   [reportsTo] INTEGER,
  87.   [birthDate] DATETIME,
  88.   [hireDate] DATETIME,
  89.   [address] NVARCHAR(70),
  90.   [city] NVARCHAR(40),
  91.   [state] NVARCHAR(40),
  92.   [country] NVARCHAR(40),
  93.   [postalCode] NVARCHAR(10),
  94.   [phone] NVARCHAR(24),
  95.   [fax] NVARCHAR(24),
  96.   [email] NVARCHAR(60),
  97.   FOREIGN KEY ([reportsTo]) REFERENCES 'employees' ([employeeId])
  98.        ON DELETE NO ACTION ON UPDATE NO ACTION
  99.   )""")
  100.  
  101.     sql_create_index = """CREATE UNIQUE INDEX contact_email
  102.   ON employees (firstName, lastName);"""
  103.  
  104.     conn = create_connection(database) # create a database connection
  105.  
  106.     if conn is not None:    # create table
  107.         create_table(conn, sql_create_table)  # create table
  108.         print("Table successfully created.")
  109.     else:
  110.         print("Error. Cannot create the database connection.")
  111.        
  112.     #inserts the first employee
  113.     conn.execute("INSERT INTO employees (employeeId, lastName, firstName, title, reportsTo, birthDate, hireDate, address, city, state, country, postalCode, phone, fax, email) \
  114.       VALUES (0, 'Cruz', 'Joao', 'CEO', 0, '1999-07-22', '2016-04-01', 'Ruskonlahdenkatu', 'Lappeenranta', 'South Karelia', 'Finland', '53850', '041 136 1841', '1234567', 'joao.cruz@company.fi')");
  115.    
  116.     for i in range(1, 200): #from 1 to 199, since 0 has already been inserted. this totals 200 insertions
  117.         line = string_generator(lastNameList, firstNameList, titleList, addressList, cityList, i)
  118.         #uncomment for debugging if needed
  119.         #print(i)
  120.         #print(line)
  121.         conn.execute(line)
  122.        
  123.     conn.commit()
  124.     print ("Populated successfully.");
  125.    
  126.     conn.execute("""CREATE INDEX employees_index \
  127.   ON employees (firstName, lastName);""")
  128.     print ("Index created successfully.");
  129.    
  130.     conn.commit()
  131.     conn.close()
  132.    
  133. if __name__ == '__main__':
  134.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement