本文整理汇总了Python中simplesqlite.SimpleSQLite.create_table_from_json方法的典型用法代码示例。如果您正苦于以下问题:Python SimpleSQLite.create_table_from_json方法的具体用法?Python SimpleSQLite.create_table_from_json怎么用?Python SimpleSQLite.create_table_from_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类simplesqlite.SimpleSQLite
的用法示例。
在下文中一共展示了SimpleSQLite.create_table_from_json方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_normal_file
# 需要导入模块: from simplesqlite import SimpleSQLite [as 别名]
# 或者: from simplesqlite.SimpleSQLite import create_table_from_json [as 别名]
def test_normal_file(
self,
tmpdir,
json_text,
filename,
table_name,
expected_table_name,
expected_attr_names,
expected_data_matrix,
):
p_db = tmpdir.join("tmp.db")
p_json = tmpdir.join(filename)
with open(str(p_json), "w") as f:
f.write(json_text)
con = SimpleSQLite(str(p_db), "w")
con.create_table_from_json(str(p_json), table_name)
assert con.fetch_table_names() == [expected_table_name]
assert expected_attr_names == con.fetch_attr_names(expected_table_name)
result = con.select(select="*", table_name=expected_table_name)
result_matrix = result.fetchall()
assert len(result_matrix) == 3
assert result_matrix == expected_data_matrix
示例2: open
# 需要导入模块: from simplesqlite import SimpleSQLite [as 别名]
# 或者: from simplesqlite.SimpleSQLite import create_table_from_json [as 别名]
# create sample data file ---
with open(file_path, "w") as f:
f.write(
"""{
"table_a" : [
{"attr_b": 4, "attr_c": "a", "attr_a": 1},
{"attr_b": 2.1, "attr_c": "bb", "attr_a": 2},
{"attr_b": 120.9, "attr_c": "ccc", "attr_a": 3}
],
"table_b" : [
{"a": 1, "b": 4},
{"a": 2 },
{"a": 3, "b": 120.9}
]
}"""
)
# create table ---
con = SimpleSQLite("sample.sqlite", "w")
con.create_table_from_json(file_path)
# output ---
for table_name in con.fetch_table_names():
print("table: " + table_name)
print(con.fetch_attr_names(table_name))
result = con.select(select="*", table_name=table_name)
for record in result.fetchall():
print(record)
print()