如何在 python 中的 sql 数据库中的一列中找到多个数字的总和?
how to find the sum of multiple numbers from a column in a sql database in python?
我有一个包含预订 table 的数据库。预订 table 中的一列是 'incomes',另一列是 'date_of_booking,',它存储'DD/MM/YYYY' 格式的日期。我正在尝试编写一个功能,让用户输入一个月,然后从中计算出该月的所有收入。到目前为止我有这个:
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sqlCmd = 'SELECT date FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
for row in conn.execute(sqlCmd):
print (row)
使用此代码,我可以输出特定月份的预订日期。但是我想输出特定月份的总收入。我需要添加什么才能计算出特定月份的总收入并将其输出?任何帮助将不胜感激,谢谢。
首先,您想 select 在您的 sql 声明中。
sqlCmd = 'SELECT date_of_booking,incomes FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
income_sum = 0
for (row_date, row_income) in conn.execute(sqlCmd):
income_sum += row_income
print row_date
print income_sum
然后您可以像上面一样在循环中指定行的日期和收入。
替换一条语句。
SELECT sum(income) FROM bookings where SUBSTR(date,4,2)='04'
如:
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('CREATE TABLE bookings (date text, income real)')
c.execute('''INSERT INTO bookings VALUES ('01/04/2017', 19.22)''')
c.execute('''INSERT INTO bookings VALUES ('15/04/2017', 19.22)''')
c.execute('''INSERT INTO bookings VALUES ('22/04/2017', 19.22)''')
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sql = '''SELECT sum(income) FROM bookings where SUBSTR(date,4,2)="%.2i"''' % int(lookForMonth)
for row in c.execute(sql):
print (row)
结果输出:
What month, please? (number from 1 through 12):4
(57.66,)
我有一个包含预订 table 的数据库。预订 table 中的一列是 'incomes',另一列是 'date_of_booking,',它存储'DD/MM/YYYY' 格式的日期。我正在尝试编写一个功能,让用户输入一个月,然后从中计算出该月的所有收入。到目前为止我有这个:
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sqlCmd = 'SELECT date FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
for row in conn.execute(sqlCmd):
print (row)
使用此代码,我可以输出特定月份的预订日期。但是我想输出特定月份的总收入。我需要添加什么才能计算出特定月份的总收入并将其输出?任何帮助将不胜感激,谢谢。
首先,您想 select 在您的 sql 声明中。
sqlCmd = 'SELECT date_of_booking,incomes FROM bookings WHERE SUBSTR(date,4,2)="%.2i"' % int(lookForMonth)
income_sum = 0
for (row_date, row_income) in conn.execute(sqlCmd):
income_sum += row_income
print row_date
print income_sum
然后您可以像上面一样在循环中指定行的日期和收入。
替换一条语句。
SELECT sum(income) FROM bookings where SUBSTR(date,4,2)='04'
如:
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('CREATE TABLE bookings (date text, income real)')
c.execute('''INSERT INTO bookings VALUES ('01/04/2017', 19.22)''')
c.execute('''INSERT INTO bookings VALUES ('15/04/2017', 19.22)''')
c.execute('''INSERT INTO bookings VALUES ('22/04/2017', 19.22)''')
validMonth = False
while not validMonth:
lookForMonth = input('What month, please? (number from 1 through 12):')
try:
validMonth = 1<=int(lookForMonth)<=12
except:
pass
sql = '''SELECT sum(income) FROM bookings where SUBSTR(date,4,2)="%.2i"''' % int(lookForMonth)
for row in c.execute(sql):
print (row)
结果输出:
What month, please? (number from 1 through 12):4
(57.66,)