-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimport.py
35 lines (30 loc) · 1.27 KB
/
import.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
import sqlite3
def main():
"""Import the acronyms created by /u/OscarRainy"""
# Open the list of acronyms that I copy/pasted from /u/OscarRainy.
with open("acronyms.txt", "r") as file:
for line in file.readlines():
# Convert the ACRONYM = NAME line into something readable.
acronym = line.split("=")[0].strip().upper()
name = line.split("=")[1].strip().title()
# Connect to the sqlite3 database.
con = sqlite3.connect('metalcore.db')
cur = con.cursor()
# Check if the acronym, name pair already exists.
cur.execute("SELECT * FROM acronyms WHERE EXISTS (SELECT * FROM acronyms WHERE acronym=:acronym AND name=:name)", {
'acronym': acronym,
'name': name,
})
results = cur.fetchall()
if not results:
# If the acronym, name pair doesn't exist, create it.
cur.execute("INSERT INTO acronyms values (:acronym, :name)", {
'acronym': acronym,
'name': name,
})
# Save the changes.
con.commit()
# Close the database connection.
con.close()
if __name__ == "__main__":
main()