如何在 Python 中遍历 cur.fetchall()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34463901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Iterate through cur.fetchall() in Python
提问by npormambi
I am working on database connectivity in Python 3.4. There are two columns in my database.
我正在研究 Python 3.4 中的数据库连接。我的数据库中有两列。
Below is the query which gives me all the data from two columns in shown format QUERY:
下面是查询,它以显示格式 QUERY 为我提供两列中的所有数据:
cur.execute(""" select * from filehash """)
data=cur.fetchall()
print(data)
OUTPUT:
输出:
[('F:\test1.py', '12345abc'), ('F:\test2.py', 'avcr123')]
To iterate through this output, my code is as below
要遍历此输出,我的代码如下
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
This gives me below error
这给了我以下错误
print(row[i])
TypeError: string indices must be integers
Let me know how can we work on individual values of fetchall()
让我知道我们如何处理个人价值观 fetchall()
回答by Redbeard011010
To iterate over and print rows from cursor.fetchall()
you'll just want to do:
要迭代并打印行,cursor.fetchall()
您只需要执行以下操作:
for row in data:
print row
You should also be able to access indices of the row, such as row[0]
, row[1]
, iirc.
您还应该能够访问行的索引,例如row[0]
、row[1]
、 iirc。
Of course, instead of printing the row, you can manipulate that row's data however you need. Imagine the cursor as a set of rows/records (that's pretty much all it is).
当然,您可以根据需要操作该行的数据,而不是打印该行。把游标想象成一组行/记录(几乎就是这样)。
回答by zvone
It looks like you have two colunms in the table, so each row will contain two elements.
看起来您在表中有两列,因此每行将包含两个元素。
It is easiest to iterate through them this way:
以这种方式遍历它们是最简单的:
for column1, column2 in data:
That is the same as:
这与:
for row in data:
column1, column2 = row
You could also, as you tried:
您也可以尝试:
for row in data:
print row[0] # or row[i]
print row[1] # or row[j]
But that failed because you overwrote i
with the value of first column, in this line: for i, row in data:
.
但是失败了,因为你改写i
与第一列的值,在这一行:for i, row in data:
。
EDIT
编辑
BTW, in general, you will never needthis pattern in Python:
顺便说一句,一般来说,在 Python 中你永远不需要这种模式:
i = 0
for ...:
...
i += 1
Instead of that, it is common to do simply:
取而代之的是,通常简单地执行以下操作:
for item in container:
# use item
# or, if you really need i:
for i, item in enumerate(container):
# use i and item
回答by repzero
looking at
看着
[('F:\test1.py', '12345abc'), ('F:\test2.py', 'avcr123')]
i j i j
you are taking a strings i and j and indexing it like
您正在使用字符串 i 和 j 并将其编入索引
print(row['F:\test1.py'])
print(row['12345abc'])
which gave you typeError
这给了你 typeError
TypeError: string indices must be integers
this is because i in data is a string and your a indexing this
这是因为数据中的 i 是一个字符串,而您的索引是这个
try this
尝试这个
for i,j in data:
print(i)
print(j)
回答by Kevin Yan
As the output you given [('F:\\test1.py', '12345abc'), ('F:\\test2.py', 'avcr123')]
作为你给出的输出 [('F:\\test1.py', '12345abc'), ('F:\\test2.py', 'avcr123')]
for i, row in data:
print(i) # i is column1's value
print(row)# row is column's value
So you don't need row[i] or row[j], that was wrong, in that each step of that iteration
所以你不需要 row[i] 或 row[j],这是错误的,因为迭代的每一步
for i, row in data
is the same as i, row = ('abc', 'def')
it set abc
to variable i
and 'def' to row
与i, row = ('abc', 'def')
它设置abc
为变量i
和 'def' 相同row
BTW ,I don't know what database you use, if you use Mysql
and python driverMySQL Connector
, you can checkout this guide to fetch mysql result as dictionaryyou can get a dict in iteration, and the keys is your table fields' name. I think this method is convenient more.
顺便说一句,我不知道您使用的是什么数据库,如果您使用Mysql
python 驱动程序MySQL Connector
,您可以查看本指南以获取 mysql 结果作为字典,您可以在迭代中获得一个字典,键是您的表字段的名称。我觉得这个方法比较方便。
回答by SANTOSH SINGH
To iterate through this: [('F:\test1.py', '12345abc'), ('F:\test2.py', 'avcr123')]
遍历这个: [('F:\test1.py', '12345abc'), ('F:\test2.py', 'avcr123')]
Code:
for i in data:
print i[0] + '\t' + i[1]
Output:
输出:
F:\test1.py 12345abc
F:\test2.py avcr123
F:\test1.py 12345abc
F:\test2.py avcr123