gpt4 book ai didi

python - Pymysql DictCursor 返回列表而不是字典

转载 作者:行者123 更新时间:2023-12-01 01:08:33 24 4
gpt4 key购买 nike

我试图弄清楚为什么这个查询返回列表列表而不是字典列表。我怀疑它与查询的连接或“as”部分有关。任何对此的见解将不胜感激。

代码如下:

   try:
#Connect to database
connection = pymysql.connect(
dbConfig.host,
user=dbConfig.user,
passwd=dbConfig.password,
db=dbConfig.db,
ssl=dbConfig.ssl,
connect_timeout=5
)
cursor = connection.cursor(pymysql.cursors.DictCursor)
print(cursor)

except Exception:
print("Error connecting to db")
return

sql = ('select retsub.sms_credits as sms_balance, '
'retsub.subscription_credit as mms_balance, '
'mg.merchants_id, '
'mg.store_name, '
'mg.email_representative from mg_groupdeals_merchants mg '
'left join mg_giftdigital_retailer_retailersubscription retsub on (mg.merchants_id=retsub.merchant_id) '
'left join mg_api_event_mapping event on(mg.merchants_id=event.merchant_id) '
'where retsub.sms_credits is not null '
'and event.merchant_id is not null '
'group by merchants_id;')

cursor.execute('select * from mg_giftdigital_user_sessions')
print(cursor.fetchall())
cursor.execute(sql)
print(cursor.fetchall())

按执行顺序返回:

第一:与column_name:value的查询相匹配的字典列表,这正是我所期望的。

第二个:像这样的列表 (('63741', '7598.7924528302',merchant_id, 'store_name'),)

最佳答案

尝试:

connection = pymysql.connect(
dbConfig.host,
user=dbConfig.user,
passwd=dbConfig.password,
db=dbConfig.db,
ssl=dbConfig.ssl,
connect_timeout=5
cursorclass=pymysql.cursors.DictCursor
)

我将光标类型更改为连接方法内

关于python - Pymysql DictCursor 返回列表而不是字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com