-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtableCreator.py
76 lines (49 loc) · 2.1 KB
/
tableCreator.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import mysql.connector as sql
from mysql.connector import errorcode
import json
import sys
database = 'STASTISTICAL_DATA_ANALYSIS'
user = 'user_name' # <<<<--- Change here
password = 'your_password' # <<<<--- Change here
branchName = "Chemical_2019" # Table Name # <<<<--- Change here
path_of_json_file = "path/to/json/file" # <<<<--- Change here
# creating table
def tableCreation():
try:
cursor.execute(f"Create table {branchName}(Rank_No int, RollNo int, Name varchar(30), Cgpa float, Sgpa float, Points int)")
except sql.Error as e:
if e.errno == 1050:
cursor.execute(f"Drop Table {branchName};")
print(f"Existing table {branchName} removed")
cursor.execute(f"Create table {branchName}(Rank_No int, RollNo int, Name varchar(30), Cgpa float, Sgpa float, Points int)")
def valueAdder():
for i, item in enumerate(data):
Rank = item.get("Rank", None)
RollNo = item.get("Rollno", None)
Name = item.get("Name", None)
Cgpa= item.get("Cgpa", None)
Sgpa= item.get("Sgpa", None)
Points = item.get("Points", None)
cursor.execute(f"INSERT INTO {branchName} (Rank_No, RollNo, Name, Cgpa, Sgpa, Points) VALUES (%s, %s, %s, %s, %s, %s)", (Rank, RollNo, Name, Cgpa, Sgpa, Points))
# Removing Duplicates Items
def duplicteValueRemover():
cursor.execute(f"CREATE TABLE copy_of_{branchName} SELECT DISTINCT Rank_no, RollNo, Name, Cgpa, Sgpa, Points FROM {branchName}")
cursor.execute(f"Drop Table {branchName};")
cursor.execute(f"Alter Table copy_of_{branchName} rename to {branchName}")
print("Duplite Value Removed.")
if __name__ == "__main__":
try:
conn = sql.connect(host='localhost',database=database,user=user,password=password)
if conn.is_connected():
print("Connected to MySql Server\n\n")
cursor = conn.cursor()
except Exception as e:
sys.exit("Sorry, Couldn't Connect to Server. Please try again later.")
with open (path_of_json_file, "r") as file:
data = json.load(file)
tableCreation()
valueAdder()
duplicteValueRemover()
conn.commit()
conn.close()
print(f"{branchName} table created.")