Repository navigation
Expand file tree
/
Copy pathconnection.py
More file actions
63 lines (47 loc) · 1.56 KB
/
Copy pathconnection.py
File metadata and controls
63 lines (47 loc) · 1.56 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
import mysql.connector
class Connection():
cnx = None
cursor = None
def __init__(self,user='root',passwd='rootroot',database='mec'):
self._cred = [user,passwd,database]
def opencnx(self):
try:
self.cnx = mysql.connector.connect(user=self._cred[0],
password=self._cred[1],
database=self._cred[2])
except mysql.connector.Error as err:
print("Error connecting to database:",err)
def closecnx(self):
self.cnx.commit()
self.cnx.close()
# self.cnx = None
def opencursor(self):
self.cursor = self.cnx.cursor()
def closecursor(self):
self.cnx.commit()
self.cursor.close()
# self.cursor = None
def query(self,query):
if (not self.cnx.is_connected()):
print("Not connected to database")
return
if (not self.cursor):
print("Cursor not connected")
return
self.cursor.execute(query)
try:
return self.cursor.fetchall()
except:
return None
if __name__ == "__main__":
db = Connection()
db.opencnx()
db.opencursor()
result = db.query("show tables")
print(result)
result = db.query("insert into testtable values(34)")
print(result)
result = db.query("select * from testtable")
print(result)
db.closecursor()
db.closecnx()