-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDBConnect.py
executable file
·98 lines (90 loc) · 1.89 KB
/
DBConnect.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
/* @DBConnect()
* @author: Alba Gómez Segura
* @date: 01/02/2017
* @description: Connection to the database source
* @Attributes:
* server;
* user;
* password;
* dataBase;
* @methods:
* __init__ (construct)
* connect
* close
* executionQueryTargets
* executionQuerySimilarity
*/
"""
import warnings
import os
from switch import Switch
import json
import _mysql
import sys
import MySQLdb
class DBConnect(object):
def __init__(self):
self.server = "database"
self.user = "user"
self.password = "userpwd"
self.dataBase = "project_fda"
return
"""
Mysql connection to the database
"""
def connect(self):
try:
self.db = MySQLdb.connect(self.server, self.user, self.password, self.dataBase)
# print "Connected successfully Project!!!\n"
except:
print("Database does not exist")
return
"""
Close mysql connection
"""
def close(self):
if (self.db):
self.cursor.close()
del self.cursor
self.db.close()
return
"""
Executes the targets query
Returns data which is a list with targets found if there are any
"""
def executionQueryTargets(self, query, organism, smile):
data = []
self.connect()
self.cursor = self.db.cursor()
try:
self.cursor.execute(query % (organism, smile))
data = self.cursor.fetchall()
except Exception as e:
print "Error: ", e
print "Query: ", query
finally:
self.close()
return data
"""
Executes the similarity query
Returns cleanData which is a list with similar molecules if there are any
"""
def executionQuerySimilarity(self, query):
data = []
cleanData = []
self.connect()
self.cursor = self.db.cursor()
try:
self.cursor.execute(query)
data = self.cursor.fetchall()
for info in data:
cleanData.append(info)
except Exception as e:
print "Error: ", e
print "Query: ", query
finally:
self.close()
return cleanData