-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mydb.py
68 lines (59 loc) · 2.2 KB
/
Mydb.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
import mysql.connector
from PyQt5.QtWidgets import QMessageBox
# noinspection PyBroadException
class Mysql:
def __init__(self, host, user, passwd):
self.mymsg = None
self.host = host
self.user = user
self.passwd = passwd
try:
self.mydb = mysql.connector.connect(
host=self.host,
user=self.user,
passwd=self.passwd
)
except:
QMessageBox.warning(None, 'ERROR', 'Unable to connect to server')
else:
self.cursor = self.mydb.cursor()
def use(self, data):
try:
self.cursor.execute('use '+data)
except:
QMessageBox.warning(None, 'ERROR', 'no table found')
def select(self, data, table):
try:
self.cursor.execute(f"select {data} from {table}")
except:
QMessageBox.warning(None, 'ERROR', 'user not found')
else:
val = self.cursor.fetchall()
return val
def insert(self, table, uid, username, password, name, birthday, cpf, status1, company,bs,ls):
try:
self.cursor.execute(
f"insert into {table} values({uid}, '{username}', '{password}', '{name}', '{birthday}', '{cpf}','{status1}','{company}',{bs},{ls})")
except:
QMessageBox.warning(None, 'warning', 'wrong values')
else:
QMessageBox.information(None, 'Done!', f'{username} registered')
def delete(self, table, uid):
try:
self.cursor.execute(f"delete from {table} where id ='{uid}'")
except:
QMessageBox.warning(None, 'ERROR', 'ID not found')
else:
QMessageBox.information(None, 'DONE!', 'user deleted')
def update(self, table, data):
try:
self.cursor.execute(f"update {table} set {data}")
except:
QMessageBox.warning(None, 'ERROR', 'could not update')
else:
QMessageBox.information(None, 'DONE!', 'data updated')
def alter(self, table, data):
try:
self.cursor.execute(f'alter table {table} {data}')
except:
QMessageBox.warning(None, 'ERROR', 'could not insert values')