Python 遍历数据库中表的列并打印其中的记录数

Python 遍历数据库中表的列并打印其中的记录数,python,mysql,database,aggregate-functions,Python,Mysql,Database,Aggregate Functions,遍历数据库中表的列并打印其中的记录数 已经连接到数据库,需要遍历表中的列并打印每个存档中的记录计数,如下所示 import mysql.connector from mysql.connector import Error try: mySQLconnection = mysql.connector.connect(host='localhost', database='nmoorg',

遍历数据库中表的列并打印其中的记录数

已经连接到数据库,需要遍历表中的列并打印每个存档中的记录计数,如下所示

import mysql.connector
from mysql.connector import Error
try:
   mySQLconnection = mysql.connector.connect(host='localhost',
                                    database='nmoorg',
                                   user='root',
                                   password='ChvU5M12')
   sql_select_Query = "select * from archive"
   cursor = mySQLconnection .cursor()
   cursor.execute(sql_select_Query)
   records = cursor.fetchall()
   print("Total number of rows in python_developers is - ",  cursor.rowcount)
   print ("Printing each row's column values i.e.  developer record")

   for row in records:
       print("archive name = ", row[1], )

   cursor.close()

except Error as e :
    print ("Error while connecting to MySQL", e )
finally:
    #closing database connection.
    if(mySQLconnection .is_connected()):
        connection.close()
        print("MySQL connection is closed")
我需要计算列而不是表中的记录数

SELECT COUNT(*) num FROM archive
是计算表中行数的方法

SELECT COUNT(*) num, COUNT(column) num_column FROM archive
是在
archive.column
中计算所有行和非空值行数的方法

我想这就是你所说的计算列而不是表中的记录数的意思

通过执行
SELECT*
进行计数,并对结果集中的行进行计数,效率极低。另外,
.fetchall()
如果表有任何有趣的大小,它将耗尽RAM。SQL的全部要点是允许您处理比RAM大几个数量级的数据集

是计算表中行数的方法

SELECT COUNT(*) num, COUNT(column) num_column FROM archive
是在
archive.column
中计算所有行和非空值行数的方法

我想这就是你所说的计算列而不是表中的记录数的意思


通过执行
SELECT*
进行计数,并对结果集中的行进行计数,效率极低。另外,
.fetchall()
如果表有任何有趣的大小,它将耗尽RAM。SQL的全部要点是允许您处理比RAM大几个数量级的数据集。

您能从表中提供一个示例行吗?我不明白你所说的列而不是表中的记录数是什么意思。你能提供一个表中的样本行吗?我不明白你所说的列而不是表中的记录数是什么意思。