forked from priyanshugandhi/Med-Help
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforDatabase.py
More file actions
executable file
·70 lines (56 loc) · 2.1 KB
/
forDatabase.py
File metadata and controls
executable file
·70 lines (56 loc) · 2.1 KB
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
import mysql.connector
import scrapeWeb
import config
def databaseHandler():
mydb = mysql.connector.connect(
host=config.host,
user=config.dbuser,
passwd=config.dbpassword
)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES") # returns all the stored databases
createDatabase = True
for each in mycursor:
if each[0] == 'treatment':
createDatabase = False
if createDatabase:
print("Creating Database ... ")
mycursor.execute("CREATE DATABASE treatment")
else:
print("Database already exists ...")
# calling the databaseHandler() to create a Database if it does not exists already
databaseHandler()
# Creating the table if it does not exists after we are sure that the Database exits
mydb = mysql.connector.connect(
host=config.host,
user=config.dbuser,
passwd=config.dbpassword,
database="treatment"
)
mycursor = mydb.cursor()
mycursor.execute(
"CREATE TABLE IF NOT EXISTS data (disease VARCHAR(255), treatment VARCHAR(255), description VARCHAR(255))")
# insert data into the database
def insertData(disease, data):
for eachType in data:
query = "INSERT INTO data (disease, treatment, description) VALUES ('{}', '{}','{}')".format(disease, eachType,
data[eachType])
mycursor.execute(query)
mydb.commit()
# returns the corresponding treatment of the disease
def showData(disease):
treatmentData = {}
mycursor.execute("SELECT * FROM data where disease = '{}'".format(disease))
myresult = mycursor.fetchall()
if len(myresult) == 0:
disease, tempData = scrapeWeb.scrapeGoogle(disease)
insertData(disease, tempData)
treatmentData = showData(disease)
else:
for eachRow in myresult:
treatmentData[eachRow[1]] = eachRow[2]
return treatmentData
# main function
def fetch(disease):
treatmentData = showData(disease.upper())
return treatmentData