-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMyAccount.py
147 lines (136 loc) · 6 KB
/
MyAccount.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from PyQt5.QtGui import *
from Ui_MyAccount import Ui_MainWindow
import sys,os
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import jaydebeapi
iflogin = True
class MyAcc(QMainWindow,Ui_MainWindow):
def __init__(self,parent=None):
super(QMainWindow, self).__init__(parent)
self.setupUi(self)
self.ifdbclose = False
try:
fo = open("un.txt", "r") # 读取已登录的用户名
except:
QMessageBox.warning(self,"","用户未登录或无法读取登录信息")
return
self.string = fo.read()
fo.close()
self.db = self.connectdb() # 连接数据库
self.cur = self.db.cursor()
self.cur.execute("SELECT * FROM eatecuser.users WHERE user_name='{}'".format(self.string))
self.data = self.cur.fetchone()
self.disp_name.setText(self.data[0])
self.disp_email.setText(self.data[7])
self.disp_time.setText(str(self.data[3]))
self.disp_vip.setText(str(self.data[4]))
self.disp_date.setText(self.data[5])
self.b_altername.clicked.connect(self.altername)
self.b_alteremail.clicked.connect(self.alteremail)
self.b_alterpassword.clicked.connect(self.alterpassword)
self.b_altertime.clicked.connect(self.cleartime)
self.b_closedb.clicked.connect(self.closedb)
self.showplaytime()
self.setWindowTitle("账号管理")
def showplaytime(self):
self.cur.execute("SELECT * FROM eatecuser.userdata")
timedata = self.cur.fetchall()
model = QStandardItemModel()
model.setHorizontalHeaderItem(0, QStandardItem("日期"))
model.setHorizontalHeaderItem(1, QStandardItem("学习时长(分钟)"))
for i in range(0,len(timedata)):
for j in range(1,3):
model.setItem(i,j-1,QStandardItem(str(timedata[i][j])))
self.table_playtime.setModel(model)
self.table_playtime.setColumnWidth(1, 150)
self.cur.execute("SELECT * FROM eatecuser.notedata")
notedata = self.cur.fetchall()
model2 = QStandardItemModel()
model2.setHorizontalHeaderItem(0, QStandardItem("日期"))
model2.setHorizontalHeaderItem(1, QStandardItem("笔记数量"))
model2.setHorizontalHeaderItem(2, QStandardItem("字数统计"))
for i in range(0, len(notedata)):
for j in range(1, 4):
model2.setItem(i, j - 1, QStandardItem(str(notedata[i][j])))
self.table_note.setModel(model2)
def altername(self):
succ=True
if self.modify_name.text()=="":
QMessageBox.warning(self,"警告","请输入修改后的用户名")
else:
self.cur.execute("SELECT * FROM eatecuser.users WHERE user_name='{}'".format(self.modify_name.text()))
t = self.cur.fetchone()
if t:
QMessageBox.warning(self, "警告", "已存在该用户")
succ = False
else:
try:
self.cur.execute("UPDATE eatecuser.users SET user_name='{}' WHERE user_name='{}'".format(self.modify_name.text(),self.string))
except:
QMessageBox.warning(self,"","修改失败")
succ=False
if succ:
QMessageBox.about(self,"","修改成功")
def alteremail(self):
succ = True
if self.modify_email.text()=="":
QMessageBox.warning(self,"警告","请输入新邮箱")
succ = False
else:
try:
self.cur.execute("UPDATE eatecuser.users SET user_email='{}' WHERE user_name='{}'".format(self.modify_email.text(),self.string))
except:
QMessageBox.warning(self,"","修改失败")
succ = False
if succ:
QMessageBox.about(self, "", "修改成功")
def alterpassword(self):
succ = True
if self.modify_password.text()=="" or self.modify_passwordold.text()=="":
QMessageBox.warning(self,"警告","请输入密码")
succ = False
else:
self.cur.execute("SELECT * FROM eatecuser.users WHERE user_name='{}'".format(self.string))
t = self.cur.fetchone()
if t[1]==self.modify_passwordold.text():
try:
self.cur.execute("UPDATE eatecuser.users SET user_password='{}' WHERE user_name='{}'"
.format(self.modify_password.text(),self.string))
except:
QMessageBox.warning(self,"未知错误","修改失败")
succ = False
else:
QMessageBox.warning(self, "警告", "旧密码错误")
succ = False
if succ:
QMessageBox.about(self,"","修改成功")
def cleartime(self):
succ = True
try:
self.cur.execute("UPDATE eatecuser.users SET play_time={} WHERE user_name='{}'"
.format('0', self.string))
except:
QMessageBox.warning(self, "未知错误", "修改失败")
succ = False
if succ:
QMessageBox.about(self, "", "修改成功")
def connectdb(self):
url = 'jdbc:postgresql://192.168.195.129:26000/eatecuser'
user = 'dbuser'
password = 'Bigdata@123'
# password = 'Gauss#3demo'
print("正在连接数据库,请稍后...")
conn = jaydebeapi.connect('org.postgresql.Driver', url, [user, password], '.\postgresql-42.3.1.jar')
print("数据库已连接")
return conn
def closedb(self):
self.db.close()
print("数据库已关闭")
self.close()
if __name__ == '__main__':
QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)#自动适配不同dpi的屏幕
app = QApplication(sys.argv)
window = MyAcc()
window.show()
sys.exit(app.exec_())