I want to convert a JSON file I created to a SQLite database.
My intention is to decide later which data container and entry point is best, json (data entry via text editor) or SQLite (data entry via spreadsheet-like GUIs like SQLiteStudio).
我的json文件如下(包含我所在城市一些十字路口的交通数据):
...
"2011-12-17 16:00": {
"local": "Av. Protásio Alves; esquina Ramiro Barcelos",
"coord": "-30.036916,-51.208093",
"sentido": "bairro-centro",
"veiculos": "automotores",
"modalidade": "semaforo 50-15",
"regime": "típico",
"pistas": "2+c",
"medicoes": [
[32, 50],
[40, 50],
[29, 50],
[32, 50],
[35, 50]
]
},
"2011-12-19 08:38": {
"local": "R. Fernandes Vieira; esquina Protásio Alves",
"coord": "-30.035535,-51.211079",
"sentido": "único",
"veiculos": "automotores",
"modalidade": "semáforo 30-70",
"regime": "típico",
"pistas": "3",
"medicoes": [
[23, 30],
[32, 30],
[33, 30],
[32, 30]
]
}
...
And I have created nice database with a one-to-many relation with these lines of Python code:
import sqlite3
db = sqlite3.connect("fluxos.sqlite")
c = db.cursor()
c.execute('''create table medicoes
(timestamp text primary key,
local text,
coord text,
sentido text,
veiculos text,
modalidade text,
pistas text)''')
c.execute('''create table valores
(id integer primary key,
quantidade integer,
tempo integer,
foreign key (id) references medicoes(timestamp))''')
但问题是,当我准备插入实际数据为c.execute("insert into medicoes values(?,?,?,?,?,?,?)" % keys)
的行时,我意识到,由于从JSON文件加载的dict没有特殊顺序,它无法正确映射到数据库的列顺序.
因此,我问:"我应该使用哪种策略/方法以编程方式从JSON文件中的每个"块"(在本例中为"local"、"coord"、"sentido"、"veiculos"、"modalidade"、"regime"、"pistas"e"medicoes")中读取键,以相同的顺序创建包含列的数据库,然后插入具有适当值的行"?
I have a fair experience with Python, but am just beginning with SQL, so I would like to have some counseling about good practices, and not necessarily a ready recipe.