-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
103 lines (92 loc) · 4.62 KB
/
Database.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
99
100
101
102
#!usr/bin/python
########################################################################################################################
# #
# File Name: Database.py #
# #
# Author: Islam Wahdan #
# #
# Description: Module to handel serialization/Retrieval of Members Data #
# #
# Language: Python #
# #
########################################################################################################################
# #
# History Description #
# #
########################################################################################################################
# #
# Version: 1.0 #
# Initial Revision #
# -------------------------------------------------------------------------------------------------------------------- #
import Defs
import PyMySQL
class MemDataBase:
""" Class for Handling Members Data """
# Active Working Members Buffer
ActiveMembers = []
Admins = []
def __init__(self):
self.DbConnection = PyMySQL.connect(host='localhost', user='adminfBZBGAJ',
password='9WhbQL6DcdlD', db='vbothome',
charset='utf8mb4', cursorclass=PyMySQL.cursors.DictCursor)
def __del__(self):
# Close Connection
self.DbConnection.close()
def AddToDB(self, member):
"""
:param member: Member Object
:return: Boolean
"""
try:
cursor = self.DbConnection.cursor()
# Create a new record
sql = "INSERT INTO `users` (`FullName`, `IsAdmin`) VALUES (%s, %s)"
cursor.execute(sql, (member.GetUserName(), member.GetAdminRights()))
# Execute
self.DbConnection.commit()
finally:
# Operation Unsuccessful
return False
# Operation Successful
return True
def RemoveFromDB(self, member):
"""
:param member: Member Object
:return: Boolean
"""
try:
cursor = self.DbConnection.cursor()
# Create a new record
sql = "DELETE FROM `bus_members` WHERE FullName = %s"
cursor.execute(sql, (member.GetUserName()))
# Execute
self.DbConnection.commit()
finally:
# Operation Unsuccessful
return False
# Operation Successful
return True
def SearchDB(self, member):
"""
:param member: Member Object
:return: UCHAR
"""
# Search in Working Buffer First
if member.GetUserName() in MemDataBase.ActiveMembers:
# Member Found
return 1
else:
# Search DB
try:
cursor = self.DbConnection.cursor()
# Create a new record
sql = "SELECT * FROM `bus_members` WHERE `FullName`=%s"
cursor.execute(sql, member.GetUserName())
result = cursor.fetchone()
finally:
# Operation Unsuccessful
return 255
if result:
return 1
else:
return 0