【发布时间】:2017-01-30 16:52:14
【问题描述】:
我在 Azure 中有一个 SQL 数据库,如下面的链接所示。我正在使用 pyodbc 在 python 中制作一个小程序来更新数据库中人员的 Elos。我制作了三个连接对象(每个看到的列一个),并使用了一个 while 循环来迭代。
它将接受用户的输入,迭代直到找到输入的 ID 代码,并且由于所有游标都在同一个 while 循环中,Rating 和 Name 游标将始终位于同一行。
https://i.stack.imgur.com/ZssyW.png
除非出于某种原因,ID 列已排序。左边的表格是Microsoft Sql Server Management Server 的截图,右边的表格是三个游标每次遍历并打印时发生的情况。
我该如何解决这个问题/是什么原因造成的?
这是我的代码(相关部分):
current_id = (ID_cursor.execute("SELECT LocalID FROM MEMBERS"))
current_rating = (rating_cursor.execute("SELECT Rating FROM MEMBERS"))
current_firstname = (firstname_cursor.execute("SELECT FirstName FROM MEMBERS"))
counter = 0
while counter < 10:
counter += 1
temp_id = ID_cursor.fetchone()[0]
temp_rating = rating_cursor.fetchone()[0]
temp_firstname = firstname_cursor.fetchone()[0]
print(temp_id, temp_firstname, temp_rating)
print("end")
【问题讨论】:
-
你为什么不直接
ORDER BY你想要的列的查询? -
我该怎么做?我无法订购评级或名称,每一行都必须保留(每个 ID 必须匹配特定的名称/评级)