小编npo*_*mbi的帖子

如何在Python中迭代cur.fetchall()

我正在使用Python 3.4中的数据库连接.我的数据库中有两列.

下面是查询,它以两种显示的格式QUERY为我提供了所有数据:

cur.execute(""" select * from filehash """)
data=cur.fetchall()
print(data)
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

[('F:\\test1.py', '12345abc'), ('F:\\test2.py', 'avcr123')]
Run Code Online (Sandbox Code Playgroud)

要遍历此输出,我的代码如下所示

cur.execute(""" select * from filehash """)
data=cur.fetchall()

i=0
j=1

for i,row in data:
    print(row[i])
    print(row[j])
    i=i+1
Run Code Online (Sandbox Code Playgroud)

这给了我以下错误

print(row[i])
TypeError: string indices must be integers
Run Code Online (Sandbox Code Playgroud)

让我知道我们如何处理个人价值观 fetchall()

python python-3.x

6
推荐指数
2
解决办法
3万
查看次数

标签 统计

python ×1

python-3.x ×1