如何使用python计算SQL数据库中的记录数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21883119/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:48:03  来源:igfitidea点击:

How to count number of records in an SQL database with python

pythonsqldatabase

提问by BigGibb

I can't seem to print the number of records in my database:
When I program:

我似乎无法打印数据库中的记录数:
当我编程时:

cursor = cnxn.cursor()   
count = cursor.execute("select count(*) from fixtures")  
cursor.commit  
print (count)

(fixtures is the name of my database)
I get:

(fixtures 是我的数据库的名称)
我得到:

pyodbc.Cursor object at 0x00000000032FC150  

...rather than the number of records.

...而不是记录数。

I am using pyodbc module on python

我在 python 上使用 pyodbc 模块

采纳答案by Martijn Pieters

For pyodbc, cursor.execute()returns the cursor object itself. You still need to retrieve the results separately.

对于 pyodbc,cursor.execute()返回游标对象本身。您仍然需要单独检索结果。

You could loop over the cursor to get rows; list()can do the looping for you and pull in all rows into a list object:

您可以遍历游标以获取行;list()可以为您执行循环并将所有行拉入列表对象:

cursor.execute("select count(*) from fixtures")  
print(list(cursor))

or you can call cursor.fetchall().

或者你可以打电话cursor.fetchall()

For a result set with just one row, you could use:

对于只有一行的结果集,您可以使用:

cursor.execute("select count(*) from fixtures")
result = cursor.fetchone()

cursor.fetchone()returns either one row, or Noneif there are no results at all.

cursor.fetchone()返回一行,或者None如果根本没有结果。

In all cases rows are sequences of columns, for a one-column result that'll be a tuple with just one value in it.

在所有情况下,行都是列的序列,对于一列结果,它将是一个只有一个值的元组。

In your example query, you are fetching a single row, with a single column, so you can get that single value with cursor.fetchone()then using indexing or tuple assignment, e.g.

在您的示例查询中,您正在获取单行,单列,因此您可以cursor.fetchone()使用索引或元组分配获得该单个值,例如

cursor.execute("select count(*) from fixtures")
fixture_count = cursor.fetchone()[0]

or

或者

cursor.execute("select count(*) from fixtures")
fixture_count, = cursor.fetchone()

You don't need to commit after a SELECT, but you didn't actually callthe commit()method either, you are missing the ()part. If you are altering data, do remember to use cursor.commit(). Note that cursor.commit()does exactly the same thing as cnxn.commit(); transactions are managed per connection, not per cursor.

您不需要在 a 之后提交SELECT,但您实际上也没有调用commit()方法,您错过了该()部分。如果您正在更改数据,请记住使用cursor.commit(). 请注意,cursor.commit()这与cnxn.commit();完全相同。事务是按连接管理的,而不是按游标管理的。

However, when not using autocommit, it is easier and better to use the connection as a context manager to ensure a transaction is aborted or committed based on there being any exceptions:

但是,当不使用 时autocommit,将连接用作上下文管理器来确保基于任何异常情况中止或提交事务会更容易和更好:

with cnxn:
    # anything in this block is handled with a transaction.

# after the block the transaction is committed, unless there was an exception.

回答by Xavi Martínez

cursor.execute("SELECT COUNT (*) FROM fixtures")
rowcount = cursor.fetchone()[0]

print (rowcount)

回答by DAE

This worked for me:

这对我有用:

tempvar = cursor.fetchall()
rowcount = len(tempvar)