PyQt5 Python:如何从 MySQL 查询结果中遍历单个数据行
PyQt5 Python: How to go through Individual Row of Data from MySQL Query Result
我正在尝试使用 Python 在 PyQt5 的 QComboBox 中添加项目。我在从每行 SQL 查询添加数据时遇到问题。
cursor = cnx.cursor()
query = "SELECT buyerID, lastName, firstName, middleName FROM buyer ORDER BY id DESC LIMIT 5"
cursor.execute(query)
data = cursor.fetchall()
item = list()
for a, b, c, d in data:
row = "{} | {}, {} {}".format(a, b, c, d)
item.append(row)
self.customerID.addItem(str(item))
这导致只有一个项目被添加到组合框中:
100000 | lastName, firstName middleName, 100000 | lastName, firstName middleName, 100000 | lastName, firstName middleName...etc.
我想在ComboBox中发生的事情是这样的(在Combo Box中总共添加5个项目)
100001 | lastName, firstName middleName
100002 | lastName, firstName middleName
100003 | lastName, firstName middleName
100004 | lastName, firstName middleName
100005 | lastName, firstName middleName
编辑:
cursor = cnx.cursor()
query = "SELECT buyerID, lastName, firstName, middleName FROM buyer ORDER BY id DESC LIMIT 5"
cursor.execute(query)
data = cursor.fetchall()
item = list()
for a, b, c, d in data:
row = "{} | {}, {} {}".format(a, b, c, d)
item.append(row)
self.customerID.addItem(str(item)) <------- I just moved this line of code into the FOR loop statement to add the item per loop.
同样的问题:
添加的appended Item仍然是所有数据行合并为一个
在发布我的问题并自行修改之后,我偶然发现了问题的答案,因为我意识到我必须更改 FOR 循环语句以枚举来自 MySQL 查询结果的数据长度和 .addItem 到 ComboBox,然后它从 data(SQL 查询结果)的结果继续到下一个元组。 =11=]
for a in enumerate(data): <--- changed the for loop statement to "enumerate" how many data inside the MySQL Query Result
j, k = a <--- split (index, tuple) in a
l, m, n, o = k <--- separate tuple into strings
string = "{} | {}, {} {}".format(l, m, n, o)
self.customerID.addItem(str(string)) <------- I just moved this line of code into the FOR loop statement to add the item before moving on to the next row in SQL results.
我正在尝试使用 Python 在 PyQt5 的 QComboBox 中添加项目。我在从每行 SQL 查询添加数据时遇到问题。
cursor = cnx.cursor()
query = "SELECT buyerID, lastName, firstName, middleName FROM buyer ORDER BY id DESC LIMIT 5"
cursor.execute(query)
data = cursor.fetchall()
item = list()
for a, b, c, d in data:
row = "{} | {}, {} {}".format(a, b, c, d)
item.append(row)
self.customerID.addItem(str(item))
这导致只有一个项目被添加到组合框中:
100000 | lastName, firstName middleName, 100000 | lastName, firstName middleName, 100000 | lastName, firstName middleName...etc.
我想在ComboBox中发生的事情是这样的(在Combo Box中总共添加5个项目)
100001 | lastName, firstName middleName
100002 | lastName, firstName middleName
100003 | lastName, firstName middleName
100004 | lastName, firstName middleName
100005 | lastName, firstName middleName
编辑:
cursor = cnx.cursor()
query = "SELECT buyerID, lastName, firstName, middleName FROM buyer ORDER BY id DESC LIMIT 5"
cursor.execute(query)
data = cursor.fetchall()
item = list()
for a, b, c, d in data:
row = "{} | {}, {} {}".format(a, b, c, d)
item.append(row)
self.customerID.addItem(str(item)) <------- I just moved this line of code into the FOR loop statement to add the item per loop.
同样的问题:
添加的appended Item仍然是所有数据行合并为一个
在发布我的问题并自行修改之后,我偶然发现了问题的答案,因为我意识到我必须更改 FOR 循环语句以枚举来自 MySQL 查询结果的数据长度和 .addItem 到 ComboBox,然后它从 data(SQL 查询结果)的结果继续到下一个元组。 =11=]
for a in enumerate(data): <--- changed the for loop statement to "enumerate" how many data inside the MySQL Query Result
j, k = a <--- split (index, tuple) in a
l, m, n, o = k <--- separate tuple into strings
string = "{} | {}, {} {}".format(l, m, n, o)
self.customerID.addItem(str(string)) <------- I just moved this line of code into the FOR loop statement to add the item before moving on to the next row in SQL results.