【发布时间】:2017-06-05 23:23:14
【问题描述】:
我正在寻找一种非显式的方式来将 JSON 列表(即 [] 与 {} 内部的项目)解析到 sqlite 数据库中。
具体来说,我卡在我想说的地方
INSERT into MYTABLE (col 1, col2, ...) this datarow in this jsondata
我觉得应该有一种方法来抽象事物,使得上面的行几乎就是它所需要的。我的数据是一个 JSON 列表,包含多个 JSON 字典。每个字典都有十几个键:值对。没有嵌套。
{"object_id":3 ,"name":"rsid" ,"column_id":1
,"system_type_id":127 ,"user_type_id":127 ,"max_length":8
,"precision":19 ,"scale":0 ,"collation_name":null
,"is_nullable":false ,"is_ansi_padded":false ,"is_rowguidcol":false
,"is_identity":false ,"is_computed":false ,"is_filestream":false
,"is_replicated":false ,"is_non_sql_subscribed":false
,"is_merge_published":false ,"is_dts_replicated":false
,"is_xml_document":false ,"xml_collection_id":0
,"default_object_id":0 ,"rule_object_id":0 ,"is_sparse":false
,"is_column_set":false}
json 列表[{k1:v1a, k2:v2a}, {k1:v1b,k2:v2b},...] 中的每个项目都将具有完全相同的 KEY 名称。我将这些作为我的 sqlite 数据库中列的名称。不过,VALUES 会有所不同。因此,通过将每个 KEY/COLUMN 的 VALUES 放入该项目的该行来填充表。
k1 | k2 | k3 | ... | km
v1a | v2a | v3a | ... | vma
v1b | v2b | v3b | ... | vmb
...
v1n | v2n | v3n | ... | vmn
在 SQL 中,插入语句不必按照与数据库列完全相同的顺序编写。这是因为您在 INSERT 声明中指定了要插入的列(以及顺序)。这对于 JSON 来说似乎是完美的,其中 JSON 列表中的每个 row/item 都包含其列名(键)。因此,我想要一个语句说“给定这行 JSON,通过将 JSON 键名与 SQL 表列名协调将其所有数据插入 SQL 表”。这就是我所说的非显式。
import json
r3 = some data file you read and close
r4 = json.loads(r3)
# let's dump this into SQLite
import sqlite3
the_database = sqlite3.connect("sys_col_database.sqlite")
the_cursor = the_database.cursor()
row_keys = r4[0].keys()
# all of the key are below for reference. 25 total keys.
'''
'is_merge_published', 'rule_object_id', 'system_type_id',
'is_xml_document', 'user_type_id', 'is_ansi_padded',
'column_id', 'is_column_set', 'scale',
'is_dts_replicated', 'object_id', 'xml_collection_id',
'max_length', 'collation_name', 'default_object_id',
'is_rowguidcol', 'precision', 'is_computed',
'is_sparse', 'is_filestream', 'name',
'is_nullable', 'is_identity', 'is_replicated',
'is_non_sql_subscribed'
'''
sys_col_table_statement = """create table sysColumns (
is_merge_published text,
rule_object_id integer,
system_type_id integer,
is_xml_document text,
user_type_id integer,
is_ansi_padded text,
column_id integer,
is_column_set text,
scale integer,
is_dts_replicated text,
object_id integer,
xml_collection_id integer,
max_length integer,
collation_name text,
default_object_id integer,
is_rowguidcol text,
precision integer,
is_computed text,
is_sparse text,
is_filestream text,
name text,
is_nullable text,
is_identity text,
is_replicated text,
is_non_sql_subscribed text
)
"""
the_cursor.execute(sys_col_table_statement)
insert_statement = """insert into sysColumns values (
{0},{1},{2},{3},{4},
{5},{6},{7},{8},{9},
{10},{11},{12},{13},{14},
{15},{16},{17},{18},{19},
{20},{21},{22},{23},{24})""".format(*r4[0].keys())
这是我卡住的地方。 insert_statement 是 executed 的字符串构造。现在我需要执行它,但要从 r4 中的每个 JSON 项中为其提供正确的数据。我不知道怎么写。
【问题讨论】: