计算元组中的行数

huangapple go评论70阅读模式
英文:

Counting the rows in tuple

问题

我从SQL中获取数据,然后使用Fetchmany()将数据放入元组中。但是我无法计算元组中的行数。以下是我正在尝试的代码。

result = cursor.execute("SELECT * FROM temp WHERE File_name = 'ABC'")
row = cursor.fetchall()
temp = len(row)
print(temp)
英文:

I am taking the data from SQL and putting the data in the tuple with Fetchmany(). But i am unable to count the rows in the tuple. Below is the code with i am trying.

result=cursor.execute("select * from temp where File_name ='ABC')

row = cursor.fetchall()

temp=row.count('FileName')

print(temp)

答案1

得分: 1

在你的代码中,看起来你试图计算fetchall()返回的行元组中的行数。然而,count()方法用于计算列表或元组中特定元素的出现次数,并且在这种情况下不会按预期工作。要计算行元组中的行数,你可以使用len()函数,它返回对象的长度(元素数量)。

result = cursor.execute("SELECT * FROM temp WHERE File_name = 'ABC'")
rows = cursor.fetchall()

row_count = len(rows)
print(row_count)
英文:

In your code, it looks like you are trying to count the rows in the row tuple returned by fetchall(). However, the count() method is used to count the occurrences of a specific element in a list or tuple, and it won't work as expected in this case. To count the number of rows in the row tuple, you can use the len() function, which returns the length of an object (number of elements).

result = cursor.execute("SELECT * FROM temp WHERE File_name = 'ABC'")
rows = cursor.fetchall()

row_count = len(rows)
print(row_count)

答案2

得分: 1

如果您想获取查询返回的行数,只需在行变量上使用len函数。

您可以在此处查看fetchall的工作原理。

row = result.fetchall()
number_of_rows = len(row)

print(number_of_rows)
英文:

If you want to get the number of rows that your query returned just use len function on your row variable.

You can see how fetchall works here

row = result.fetchall()
number_of_rows = len(row)

print(number_of_rows)

huangapple
  • 本文由 发表于 2023年6月5日 20:19:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定