Sometimes we need get the value of the MySQL aggregate functions like COUNT, MAX, etc.
For example, if I need to know how many records has a table, I could write the following code:
mysql_connect = mysql.connector.connect(user=’ernest’, database=’magazine’)
sql = ‘SELECT COUNT(*) FROM author’
cursor = mysql_connect.cursor()
cursor.execute(sql)
data = cursor.fetchall()
print(data)
This will print:
[(20,)]
Two questions:
1. How to get the real 20 records value?
2. If this the correct code to get the values of the MySQL aggregate functions?
Thank you in advanced!
Ernest
For example, if I need to know how many records has a table, I could write the following code:
mysql_connect = mysql.connector.connect(user=’ernest’, database=’magazine’)
sql = ‘SELECT COUNT(*) FROM author’
cursor = mysql_connect.cursor()
cursor.execute(sql)
data = cursor.fetchall()
print(data)
This will print:
[(20,)]
Two questions:
1. How to get the real 20 records value?
2. If this the correct code to get the values of the MySQL aggregate functions?
Thank you in advanced!
Ernest