-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.py
54 lines (48 loc) · 1.51 KB
/
importer.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import mysql.connector
import pandas as pd
from text import TextColor as tc
HOST = "localhost"
USER = "root"
PASSWORD = ""
DATABASE = "web_pemilu_fosti"
EXCEL_FILE = "data.xlsx"
def connect_mysql(host, user, password, database):
db = mysql.connector.connect(
host=host,
user=user,
password=password,
database=database
)
return db
def insert_participant(email):
cursor = db.cursor()
sql = "INSERT INTO emails (email, created_at, updated_at) VALUES (%s, NOW(), NOW())"
cursor.execute(sql, (email, ))
db.commit()
cursor.close()
if __name__ == "__main__":
try:
db = connect_mysql(
host=HOST,
user=USER,
password=PASSWORD,
database=DATABASE
)
print(f"{tc.GREEN}Success: Connected to database{tc.END}")
except:
print(f"{tc.RED}Error: Can't connect to database{tc.END}")
exit(1)
df = pd.read_excel(EXCEL_FILE)
participants = tuple(df[['name', 'student_id', 'email']].values)
for name, student_id, email in participants:
try:
student_id = student_id.upper()
name = name.title()
email = email.replace(" ", "")
print(f"{tc.YELLOW}Action: Inserting {student_id} {name}{tc.END}")
insert_participant(email)
print(f"{tc.GREEN}Success: Inserting {student_id} {name}{tc.END}")
except:
print(f"{tc.RED}Error: Inserting {student_id} {name}{tc.END}")
continue
db.close()