Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从sql数据库游标构建json数据_Python_Sql_Json - Fatal编程技术网

Python 从sql数据库游标构建json数据

Python 从sql数据库游标构建json数据,python,sql,json,Python,Sql,Json,在不知道json结构的情况下,如何从数据库查询返回json对象?所有的信息都在那个里,我只是不知道如何构建这个对象 import MySQLdb import json db = MySQLdb.connect( host, user, password, db) cursor = db.cursor() cursor.execute( query ) rows = cursor.fetchall() field_names = [i[0] for i in cursor.descriptio

在不知道json结构的情况下,如何从数据库查询返回json对象?所有的信息都在那个里,我只是不知道如何构建这个对象

import MySQLdb
import json
db = MySQLdb.connect( host, user, password, db)
cursor = db.cursor()
cursor.execute( query )
rows = cursor.fetchall()

field_names = [i[0] for i in cursor.description]
json_string = json.dumps( dict(rows) )

print field_names[0]
print field_names[1]
print json_string

db.close()
计数

严重性

{“321”:“7.2”,“1”:“5.0”,“5”:“4.3”,“7”:“6.8”,“1447”:“9.3”,“176”:“10.0”}

json对象看起来像:

{"data":[{"count":"321","severity":"7.2"},{"count":"1","severity":"5.0"},{"count":"5","severity":"4.3"},{"count":"7","severity":"6.8"},{"count":"1447","severity":"9.3"},{"count":"176","severity":"10.0"}]}
1-您可以使用
DictCursor

import pymysql
connection = pymysql.connect(db="test")
cursor = connection.cursor(pymysql.cursors.DictCursor)
cursor.execute("SELECT ...")
row = cursor.fetchone()
print row["key"]
2-MySQLdb还包括您可以使用的。建立连接时需要传递
cursorclass=MySQLdb.cursors.DictCursor

import MySQLdb
import MySQLdb.cursors
connection = MySQLdb.connect(db="test",cursorclass=MySQLdb.cursors.DictCursor)
cursor = connection.cursor()
cursor.execute("SELECT ...")
row = cursor.fetchone()
print row["key"]
1-您可以使用
DictCursor

import pymysql
connection = pymysql.connect(db="test")
cursor = connection.cursor(pymysql.cursors.DictCursor)
cursor.execute("SELECT ...")
row = cursor.fetchone()
print row["key"]
2-MySQLdb还包括您可以使用的。建立连接时需要传递
cursorclass=MySQLdb.cursors.DictCursor

import MySQLdb
import MySQLdb.cursors
connection = MySQLdb.connect(db="test",cursorclass=MySQLdb.cursors.DictCursor)
cursor = connection.cursor()
cursor.execute("SELECT ...")
row = cursor.fetchone()
print row["key"]

您遇到的问题是因为您只将获取的项转换为dict,而不进行描述

python中的
dict
需要另一个dict,或者返回两个项元组的iterable,其中每个元组的第一个项是键,第二个项是值

因为您只获取两列,所以对于每个获取的行,第一列(计数)作为键,第二列(严重性)作为值

您要做的是将这些描述结合起来,如下所示:

json_string = json.dumps([
{description: value for description, value in zip(field_names, row)}
for row in rows])

您遇到的问题是因为您只将获取的项转换为dict,而不进行描述

python中的
dict
需要另一个dict,或者返回两个项元组的iterable,其中每个元组的第一个项是键,第二个项是值

因为您只获取两列,所以对于每个获取的行,第一列(计数)作为键,第二列(严重性)作为值

您要做的是将这些描述结合起来,如下所示:

json_string = json.dumps([
{description: value for description, value in zip(field_names, row)}
for row in rows])

我使用Collections库实现了这一点,尽管代码令人困惑:

import MySQLdb
import json
import collections
db = MySQLdb.connect(host, user, passwd, db)
cursor = db.cursor()
cursor.execute( query )
rows = cursor.fetchall()
field_names = [i[0] for i in cursor.description]

objects_list = []
for row in rows:
    d = collections.OrderedDict()
    d[ field_names[0] ] = row[0]
    d[ field_names[1] ] = row[1]
    objects_list.append(d)

json_string = json.dumps( objects_list )

print json_string

db.close()
[{“计数”:176,“严重性”:“10.0”},{“计数”:1447,“严重性”:“9.3”},{“计数”:321,“严重性”:“7.2”},{“计数”:7,“严重性”:“6.8”},{“计数”:1,“严重性”:“5.8”},{“计数”:1,“严重性”:“5.0”},{“计数”:5,“严重性”:“4.3”}]


我使用Collections库实现了这一点,尽管代码令人困惑:

import MySQLdb
import json
import collections
db = MySQLdb.connect(host, user, passwd, db)
cursor = db.cursor()
cursor.execute( query )
rows = cursor.fetchall()
field_names = [i[0] for i in cursor.description]

objects_list = []
for row in rows:
    d = collections.OrderedDict()
    d[ field_names[0] ] = row[0]
    d[ field_names[1] ] = row[1]
    objects_list.append(d)

json_string = json.dumps( objects_list )

print json_string

db.close()
[{“计数”:176,“严重性”:“10.0”},{“计数”:1447,“严重性”:“9.3”},{“计数”:321,“严重性”:“7.2”},{“计数”:7,“严重性”:“6.8”},{“计数”:1,“严重性”:“5.8”},{“计数”:1,“严重性”:“5.0”},{“计数”:5,“严重性”:“4.3”}]