def login_change(self): print(str(self.admin_username.get())) print(str(self.admin_pass.get())) admin_pass = None # 数据库操作 查询管理员表 db = pymysql.connect(host='localhost', port=3306, db='crime', user='qingying', password='123456' ) sql = "SELECT * FROM admin_id WHERE admin_id = '%s'" % (self.admin_username.get()) # SQL 查询语句 try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: admin_id = row[0] admin_pass = row[1] # 打印结果 print("admin_id=%s,admin_pass=%s" % (admin_id, admin_pass)) except: print("Error: unable to fecth data") messagebox.showinfo('警告!', '用户名或密码不正确!') db.close() # 关闭数据库连接 print("正在登陆管理员修改密码界面") print("self", self.admin_pass) print("local", admin_pass) if self.admin_pass.get() == admin_pass: AdminChange(self.window) else: messagebox.showinfo('警告!', '用户名或密码不正确!')
时间: 2024-02-14 09:35:19 浏览: 16
这段代码是一个 GUI 界面的登录函数,用于验证管理员的身份并打开一个密码修改窗口。函数首先获取输入框中管理员用户名和密码,然后连接本地 MySQL 数据库 `crime` 并查询 `admin_id` 表,找到对应的管理员 ID 和密码。如果查询成功,将查询结果打印出来;否则,显示警告信息框,提示用户名或密码不正确。最后,判断用户输入的密码与数据库中查询到的密码是否一致。如果一致,打开一个密码修改窗口;否则,显示警告信息框,提示用户名或密码不正确。
需要注意的是,这段代码中的 `cursor` 对象没有定义,需要在函数中初始化并传入。另外,建议使用参数化查询语句,可以避免 SQL 注入攻击。
相关问题
def __str__(self): return str(self.data)是什么意思
这是一个 Python 类中的特殊方法,用于返回该类的实例的字符串表示。具体来说,当我们使用 print() 函数打印一个类的实例时,实际上是调用了该实例的 __str__() 方法来获取其字符串表示。
class FixedSizeHeap: def __init__(self, max_size): self.max_size = max_size self.heap = [] def push(self, item): if len(self.heap) < self.max_size: heapq.heappush(self.heap, item) else: min_item = heapq.heappop(self.heap) if item > min_item: heapq.heappush(self.heap, item) else: heapq.heappush(self.heap, min_item) def pop(self): return heapq.heappop(self.heap) def __len__(self): return len(self.heap)对于这样的类怎么打印类中的元素
要打印类中的元素,可以通过实现`__str__`方法来实现。`__str__`方法应该返回一个字符串,该字符串包含对象的信息。例如,以下代码演示了如何在类中实现`__str__`方法以打印堆的元素:
```
import heapq
class FixedSizeHeap:
def __init__(self, max_size):
self.max_size = max_size
self.heap = []
def push(self, item):
if len(self.heap) < self.max_size:
heapq.heappush(self.heap, item)
else:
min_item = heapq.heappop(self.heap)
if item > min_item:
heapq.heappush(self.heap, item)
else:
heapq.heappush(self.heap, min_item)
def pop(self):
return heapq.heappop(self.heap)
def __len__(self):
return len(self.heap)
def __str__(self):
return str(self.heap)
new_heap = FixedSizeHeap(5)
new_heap.push(1)
new_heap.push(2)
new_heap.push(3)
new_heap.push(4)
new_heap.push(5)
print(new_heap) # 输出 [1, 2, 3, 4, 5]
```
在这个例子中,我们定义了`__str__`方法来返回堆的元素。我们将`self.heap`转换为字符串并返回。现在,当我们打印`new_heap`时,它将打印堆中的元素。