Python 如何将带有动态键的dict链接到sqlite3
我需要关于如何将动态密钥链接到sqlite3 db的专业知识。 当前,我的方法产生的db在执行后将为空Python 如何将带有动态键的dict链接到sqlite3,python,json,sqlite,dictionary,Python,Json,Sqlite,Dictionary,我需要关于如何将动态密钥链接到sqlite3 db的专业知识。 当前,我的方法产生的db在执行后将为空 ```python import requests import json import sqlite3 # establishing DB and sqlite3 connection = sqlite3.connect("test.db") cursor = connection.cursor()
```python
import requests
import json
import sqlite3
# establishing DB and sqlite3
connection = sqlite3.connect("test.db")
cursor = connection.cursor()
# getting data from the API and directly to the key I´m interested in (level)
url = "https://www.alphavantage.co/query?function=FX_INTRADAY&from_symbol=EUR&to_symbol=USD&interval=5min&apikey=demo"
res = requests.get(url)
alphadata = res.json()
level = alphadata["Time Series FX (5min)"]
# Structure = {"TIME Series FX(5min)": {Dynamic Timestamp: {"1. open": value,"2. high": value,"3. low": value,"4. close": value}
start = sorted(level.keys())
next = level.values()
# I figured out how I can increase in a loop the index-number of 'alphadata' (0-99) and print out the result
for i in range(len(start)):
attempt = alphadata["Time Series FX (5min)"][start[i]]
d1 = list(attempt.values())[0]
d2 = list(attempt.values())[1]
d3 = list(attempt.values())[2]
d4 = list(attempt.values())[3]
print(i+1, start[i], d1, d2, d3, d4)
# I guess I have to link SQLITE3-DB here somehow to the above index-table, but how? I tried it the following way resulting in an empty DB:
sql_instruction = """
CREATE TABLE IF NOT EXISTS eurusd (
id INTEGER NOT NULL PRIMARY KEY,
"Date" TEXT,
"1. open" REAL,
"2. high" REAL,
"3. low" REAL,
"4. close" REAL
); """
cursor.execute(sql_instruction)
connection.commit()
for i in range(len(start)):
attempt = alphadata["Time Series FX (5min)"][start[i]]
d1 = list(attempt.values())[0]
d2 = list(attempt.values())[1]
d3 = list(attempt.values())[2]
d4 = list(attempt.values())[3]
cursor.execute("INSERT INTO eurusd VALUES(?, ?, ?, ?, ?, ?)", (i+1, start[i], d1, d2, d3, d4))
connection.commit()
# complete list of time and values incl. keys for control-purposes
# for start, next in level.items():
# print(start, next)
有人知道我如何处理SQLITE3DB的插入,因为依赖的动态键显然是基于API请求的时间的?
如果有人能在这里提供建议,我将不胜感激。
提前谢谢你 我尝试了代码,数据库不是空的。我在没有任何编辑的情况下再次尝试,它正在工作。我不知道发生了什么变化。。。。非常感谢。