dbCursor = dbConnection.execute("SELECT compid, " + rowToUse + ", nameshort, namefull FROM " + tableToUse + " WHERE " + rowToUse + " IS NOT NULL")
dbCursor1 = dbConnection.execute("SELECT compid, " + rowToUse + ", nameshort, namefull FROM " + tableToUse + " WHERE " + rowToUse + " IS NOT NULL")
myList = dbCursor1.fetchall()
myRange = [i for i in range(len(myList))] # [[i] for i in range(len(myList))]
myRangeCurrent = 0
for row1 in dbCursor:
row = myList[myRange[myRangeCurrent]]
myRangeCurrent = myRangeCurrent + 1
给我:
Traceback (most recent call last): File "<pyshell#176>", line 1, in
<module>
row = myList[myRange[myRangeCurrent]] IndexError: list index out of range
范围应该与在相同的选择查询上完成的周期完全相同。或者不是?
答案 0 :(得分:0)
检查您的SQL查询是否正确(提示:您只需要一个游标,因为您的查询相同)并打印出fetchall
的结果,然后尝试打印出行及其索引。
for i, row in enumerate(dbCursor.fetchall()):
print(i, row)
虽然,你也应该能够做到这一点
for i, row in enumerate(dbCursor):
print(i, row)
答案 1 :(得分:0)
cricket_007解释说从光标获取len
会改变光标,所以我更改了一个光标添加代码。现在不再有out of range
:
mySQL = "SELECT compid, " + rowToUse + ", nameshort, namefull FROM " + tableToUse + " WHERE " + rowToUse + " IS NOT NULL"
dbCursor = dbConnection.execute(mySQL)
dbCursor1 = dbConnection.execute(mySQL)
dbCursor2 = dbConnection.execute(mySQL)
myList = dbCursor1.fetchall()
myList2 = dbCursor2.fetchall()
myRange = list(range(len(myList2)))
myRangeCurrent = 0
for row1 in dbCursor:
row = myList[myRange[myRangeCurrent]]
myRangeCurrent = myRangeCurrent + 1