将嵌套的json对象发送到sqlite db

时间:2015-06-12 23:03:08

标签: python json sqlite

我有一个大型的json对象文件,每行一个对象。我想将此数据发送到sqlite数据库。每个json对象的格式如下:

{"1": {"google": 5},
 "2": "",
 "3": 0.0,
 "4": [10.0, 20.0, 30.0, 20.0],
 "6": "",
 "7": 2,
 "8": {},
 "9": 1.0,
 "10": 0.0}

我尝试了此代码并收到以下错误:

query = "insert into daily values (?,?,?,?,?,?,?,?,?,?)"

columns = ['1', '2', '3', '4',
           '5', '6','7',
           '8', '9', '10']

with open(JSON_FILE) as f:
    head = islice(f, 5)
    for x in head:
        line = json.loads(x)
        keys = tuple(line[c] for c in columns)
        c.execute(query, keys)

InterfaceError: Error binding parameter 0 - probably unsupported type.

两个问题:

1)为什么我收到此错误?我认为这与第一个hey的值是嵌套字典有关,但我不确定如何解决它。

2)目前我创建了这样的表:

c.execute('''create table daily
         (1 text,
          2 text,
          3 text,
          4 text,
          5 text,
          6 text,
          7 text,
          8 text,
          9 text,
          10 text)''')

理想情况下,我希望为对象中的第一个键的key:value对分别设置列。同样,我认为这是可能的,但我不太确定如何做到这一点。

1 个答案:

答案 0 :(得分:0)

如果您有Function IsInArray(myArray As Variant, val As String) As Boolean Dim i As Integer, found As Boolean found = False If Not Len(Join(myArray)) > 0 Then found = False Else For i = 0 To UBound(myArray) If myArray(i) = val Then found = True End If Next i End If IsInArray = found End Function 连接到正在运行的服务器,只需MongoClient insert进入数据库:

json

要简单地检索条目,请使用数据库client.db.collection.insert(json) # insert the json in the collection of a database db 的给定find的{​​{1}}方法:

collection