我使用 Python 和 Flask 的后端將JSON 資料拆分為各種端點,以便在我的客戶端 Swift 應用程式中檢索,而不必在客戶端下載完整的資料。
JSON檔案:
{
"pilots": [
{
"cid": 1234567,
"name": "John Smith",
"callsign": "TIA1",
"server": "USA-WEST",
"pilot_rating": 3,
"latitude": 18.42663,
"longitude": 116.15007,
"altitude": 41038,
"groundspeed": 435,
"transponder": "2200",
"heading": 154,
"qnh_i_hg": 29.96,
"qnh_mb": 1015,
"flight_plan": {
"flight_rules": "I",
"aircraft": "B737/M-VGDW/C",
"aircraft_faa": "B737/L",
"aircraft_short": "B737",
"departure": "LTBA",
"arrival": "WAMM",
"alternate": "",
"cruise_tas": "437",
"altitude": "33000",
"deptime": "1230",
"enroute_time": "1415",
"fuel_time": "1542",
"remarks": "PBN/B1D1O1S1 DOF/221107 REG/VPCTY EET/LTAA0020 UDDD0137 UBBA0151 UTAK0222 UTAA0247 UTAV0309 UTSD0322 UTTR0345 UAII0352 UTTR0354 UCFO0412 UCFM0434 ZWUQ0451 ZLHW0606 ZPKM0741 ZGZU0856 VHHK0946 RPHI1020 WAAF1251 SEL/EJKS CODE/ADF5D2 OPR/TEXAS AIR LLC ORGN/KCHIUALE PER/C RMK/CALLSIGN \"TEXAS\" /V/",
"route": "ASMAP UL333 SIV UA4 ERZ UB374 INDUR N449 DUKAN A480 KRS B701 TUGTA A909 BABUM A477 POGON L143 TISIB L141 KAMUD W186 SADAN Y1 OMBON B330 AVPAM A599 POU B330 CH A583 ZAM A461 BONDA",
"revision_id": 4,
"assigned_transponder": "0363"
},
"logon_time": "2022-11-06T07:07:42.1130925Z",
"last_updated": "2022-11-07T22:36:19.1087668Z"
}
}
我將 JSON 匯入到各種 SQLite 表中。JSON 資料每 60 秒更新一次,因此我需要相應地更新我的副本。我目前的解決方案是洗掉資料庫中的資料然后重新插入,但這很可能不是正確的方法。我不確定我應該如何將資料庫中的記錄與最新的 JSON 進行比較,我可以檢索所有記錄然后逐行比較新舊記錄,但這可能效率更低。在 Python 中執行此操作的可靠方法是什么?
插入飛行員和相關飛行計劃的代碼:
def _store_pilots(pilots):
"""Removes all records from the db, then stores pilots and associated flight plans, checking for duplicate CIDs."""
pilots_list = []
cid_list = []
fp_list = []
for pilot in pilots:
cid = int(pilot['cid'])
if cid in cid_list:
continue
cid_list.append(cid)
pilot_tuple = (
pilot['cid'], pilot['name'],
pilot['callsign'], pilot['server'],
pilot['pilot_rating'],
pilot['latitude'], pilot['longitude'],
pilot['altitude'], pilot['groundspeed'],
pilot['transponder'], pilot['heading'],
pilot['qnh_i_hg'], pilot['qnh_mb'],
pilot['logon_time'], pilot['last_updated']
)
pilots_list.append(pilot_tuple)
if pilot['flight_plan']:
fp = pilot['flight_plan']
fp_tuple = (
pilot['cid'], fp['flight_rules'],
fp['aircraft'], fp['aircraft_faa'], fp['aircraft_short'],
fp['departure'], fp['arrival'], fp['alternate'],
fp['cruise_tas'], fp['altitude'],
fp['deptime'], fp['enroute_time'], fp['fuel_time'],
fp['remarks'], fp['route'], fp['revision_id']
)
fp_list.append(fp_tuple)
with sqlite3.connect(DATABASE_PATH) as connection:
connection.execute("PRAGMA foreign_keys = 1")
c = connection.cursor()
c.execute("DELETE FROM pilots")
c.executemany("""
INSERT INTO pilots(cid, name, callsign, server, pilot_rating, latitude, longitude, altitude, groundspeed, transponder, heading, qnh_i_hg, qnh_mb, logon_time, last_updated)
values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", pilots_list)
c.executemany("""
INSERT INTO flight_plans VALUES (?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?
)
""", fp_list)
uj5u.com熱心網友回復:
您可以在/之前創建包含cid、raw_json_value和json_hash檢查hash值的表。這是一個例子:updateinsert
data = [{'cid': 1, ...}, {'cid': 2, ...}, {'cid': 3, ...}]
for item in data: # type: dict
cid = item['cid']
json_hash = hash(json.dumps(data))
# Record - let's say a model from db
record = get_record_by_cid(cid)
if not record:
save_record(cid, item, json_hash)
continue
if record.json_hash != json_hash:
update_record(cid, item, json_hash)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/533333.html
