rows = cur.fetchall() self.tableWidget.setRowCount(len(rows)) for i, row in enumerate(rows): for j, value in enumerate(row): if j == 0: value = os.path.basename(value) item = QTableWidgetItem(str(value)) self.tableWidget.setItem(i, j, item) else: item = QTableWidgetItem(str(value)) self.tableWidget.setItem(i, j, item)
时间: 2024-02-10 10:35:28 浏览: 80
这段代码是将从数据库中查询到的数据展示在 GUI 界面的表格中,具体来说,它执行了以下操作:
1. 使用 `cur.fetchall()` 获取查询结果集中的所有行数据,并将其存储在 `rows` 变量中。
2. 使用 `self.tableWidget.setRowCount(len(rows))` 设置表格中的行数为查询结果集中的行数。
3. 使用 `enumerate()` 函数遍历查询结果集中的每一行数据,并将其填充到表格中。在遍历行数据时,使用 `enumerate()` 函数获取行号 `i`,并将其作为表格中的行索引。然后,再使用 `enumerate()` 函数遍历该行数据中的每一个字段,并将其填充到表格中。
4. 对于每一个字段,如果是第一个字段,即 `j == 0` 时,使用 `os.path.basename()` 函数获取文件名,并将其作为字符串类型的 `value` 存储在 `item` 变量中。否则,直接将该字段值存储在 `item` 变量中。
5. 最后,使用 `self.tableWidget.setItem(i, j, item)` 将 `item` 变量中的值设置为表格中的单元格值。
相关问题
def login_in(self): account = self.ui.lineEdit_L_account.text() password = self.ui.lineEdit_L_password.text() account_list = [] password_list = [] conn = pymysql.connect( user='root', password='796691xjl@', host='127.0.0.1', database='mysql', port=3306 ) cur = conn.cursor() cur.execute('SELECT * FROM films.manger') rows = cur.fetchall() for row in rows: account_list.append(row[0]) password_list.append(row[1]) print(account_list, password_list) conn.commit() conn.close() for i in range(len(account_list)): if len(account) == 0 or len(password) == 0: self.ui.stackedWidget.setCurrentIndex(1) elif account == account_list[i] and password == password_list[i]: global manger_now manger_now = account self.win = MainWindow() self.close() else: self.ui.stackedWidget.setCurrentIndex(2) print("wrong")
这段代码的作用是连接 MySQL 数据库,查询 manger 表中的账号和密码数据,并与用户输入的账号和密码进行比较,如果匹配则显示 MainWindow 窗口,否则显示错误提示。
在这段代码中,我发现有一个问题,就是如果 manger 表中没有任何数据,account_list 和 password_list 都将为空列表,此时 for 循环将不会执行任何操作,因此登录过程将会失败。为了解决这个问题,你可以在 for 循环前添加一个 if 语句来检查是否有数据,例如:
```
cur.execute('SELECT * FROM films.manger')
rows = cur.fetchall()
if len(rows) == 0:
self.ui.stackedWidget.setCurrentIndex(2)
print("No data found")
else:
for row in rows:
account_list.append(row[0])
password_list.append(row[1])
for i in range(len(account_list)):
# 省略后续代码
```
这样修改后,如果 manger 表中没有任何数据,登录界面将会显示错误提示。
def query(self, sql): # 执行 SQL 查询语句 self.cursor.execute(sql) # 获取查询结果并显示在表格中 rows = self.cursor.fetchall() if rows: self.table.setRowCount(len(rows)) self.table.setColumnCount(len(rows[0])) for i, row in enumerate(rows): for j, col in enumerate(row): item = QTableWidgetItem(str(col)) self.table.setItem(i, j, item)如何设置表格每列的名字
你可以使用 `setHorizontalHeaderLabels` 方法来设置表格每列的名字。例如,如果你想将表格的第一列命名为 "姓名",第二列命名为 "年龄",第三列命名为 "性别",你可以这样写:
```
self.table.setHorizontalHeaderLabels(["姓名", "年龄", "性别"])
```
将上述代码放到 `query` 方法中,在设置表格数据之前调用即可。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)