-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBPopulationScript.py
36 lines (30 loc) · 1.07 KB
/
DBPopulationScript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import sqlite3
connection = sqlite3.connect("names.db")
cursor = connection.cursor()
cursor.execute('DROP TABLE IF EXISTS names ')
cursor.execute('CREATE TABLE names (name TEXT)')
cursor.execute('DROP TABLE IF EXISTS lastnames ')
cursor.execute('CREATE TABLE lastnames (lastname TEXT)')
try:
nameFile = open("resources/firstname.txt", 'r')
print("Opening names file.")
names = nameFile.read().split("\n")
print("Populating 'names' table...")
for name in names:
cursor.execute("INSERT INTO names (name) VALUES (?)", [name])
print("Done")
except Exception:
print("Can't open file (name).")
try:
lastnameFile = open("resources/lastname.txt", 'r')
print("Opening lastnames file.")
lastnames = lastnameFile.read().split("\n")
print("Populating 'lastnames' table...")
for lastname in lastnames:
cursor.execute("INSERT INTO lastnames (lastname) VALUES (?)", [lastname])
print("Done")
except Exception:
print("Can't open file (lastname).")
connection.commit()
cursor.close()
connection.close()