【发布时间】:2018-07-06 09:38:30
【问题描述】:
我是 Python 和 PyMySQL 的新手,所以我可能有一些配置错误。
我正在毫无问题地连接到 MySQL。我在一张桌子上进行了 SELECT 和 DESC 测试,并且能够查看结果。
我现在有一个查询,我将日期参数替换为并希望返回列的计数(客户)和客户总数乘以一个值。
客户数量返回正确,但产品计算返回无。在执行查询之前,我将其打印到控制台并复制到 MySQLWorkbench 以运行,并返回正确的值。
在我的 main 模块中,我连接到数据库并获得一个游标。然后我获取要在查询中使用的日期值并调用执行查询的函数。
connection = dbConnection()
cursor = connection.cursor()
startDate = input("enter start date (yyyy-mm-dd): ").strip()
endDate = input("enter end date (yyyy-mm-dd): ").strip()
my_queries.queryTotals(cursor, startDate, endDate)
connection.close()
在我的 my_queries 模块中,我有查询并将输入的日期替换为查询字符串,然后执行查询并获取结果:
totalsSQL = '''select
@total:=count(cus.customer_id) as customers, format(@total * 1.99, 2) as total
from customer cus
join membership mem on mem.membership_id=cus.current_membership_id
where mem.request='START'
and (mem.purchase_date > (unix_timestamp(date('{}'))*1000) and mem.purchase_date < unix_timestamp(date('{}'))*1000);'''
formattedSQL = totalsSQL.format(startDate, endDate)
cursor.execute(formattedSQL)
result = cursor.fetchone()
我得到 (32, None) 的结果,而不是获取第二列值的数值。
我在这里错过了什么?
谢谢。
【问题讨论】:
标签: mysql python-3.x pymysql