psycopg2 postgres sql 求和和计数 returns 格式不符合 json.dump
psycopg2 postgres sql request of sum and count returns format which is not respecting json.dump
我有以下查询:
import psycopg2
import simplejson as json
payload = {
"type": type,
"creators": None,
"type_count": None,
"total_monitary_action_count": None
}
cur.execute('''
SELECT SUM(currentmonitaryactioncount) as totalmonitaryactioncount , COUNT(*) as totalnumberofcreators
FROM users_contentcreatorusers
WHERE approvedcreator = true AND ''' + str(type) + ''' = true
''')
然后
record = cur.fetchall()
record = json.dumps(record)
print('here is the record dump thing')
print(record)
payload["total_monitary_action_count"] = record[0]
payload["type_count"] = record[1]
打印出来
here is the record dump thing
[[102923243, 2043]]
这是我的
的结果
payload["total_monitary_action_count"] = record[0]
payload["type_count"] = record[1]
'type_count': '[', 'total_monitary_action_count': '['}
现在我看到了双 [] 我尝试 record[0][0]
但没有积极效果。
我哪里做错了。
由于结果是列表中的列表,因此您应该将数据检索为
payload["total_monitary_action_count"] = record[0][0]
payload["type_count"] = record[0][1]
并删除将序列化结果的以下行,在这种情况下我不明白为什么需要它。
record = json.dumps(record)
我有以下查询:
import psycopg2
import simplejson as json
payload = {
"type": type,
"creators": None,
"type_count": None,
"total_monitary_action_count": None
}
cur.execute('''
SELECT SUM(currentmonitaryactioncount) as totalmonitaryactioncount , COUNT(*) as totalnumberofcreators
FROM users_contentcreatorusers
WHERE approvedcreator = true AND ''' + str(type) + ''' = true
''')
然后
record = cur.fetchall()
record = json.dumps(record)
print('here is the record dump thing')
print(record)
payload["total_monitary_action_count"] = record[0]
payload["type_count"] = record[1]
打印出来
here is the record dump thing
[[102923243, 2043]]
这是我的
的结果payload["total_monitary_action_count"] = record[0]
payload["type_count"] = record[1]
'type_count': '[', 'total_monitary_action_count': '['}
现在我看到了双 [] 我尝试 record[0][0]
但没有积极效果。
我哪里做错了。
由于结果是列表中的列表,因此您应该将数据检索为
payload["total_monitary_action_count"] = record[0][0]
payload["type_count"] = record[0][1]
并删除将序列化结果的以下行,在这种情况下我不明白为什么需要它。
record = json.dumps(record)