Python 通过pandas to_sql命令-Flask检查数据库插入结果
我有这个功能:Python 通过pandas to_sql命令-Flask检查数据库插入结果,python,pandas,sqlite,Python,Pandas,Sqlite,我有这个功能: def getHistoricRates(): """ Here we have the function that will retrieve the historical rates from fixer.io, since 1999 """ rates = [] response = urlopen('my_key') data = response.read() rdata = json.loads(data.decode(), p
def getHistoricRates():
""" Here we have the function that will retrieve the historical rates from fixer.io, since 1999 """
rates = []
response = urlopen('my_key')
data = response.read()
rdata = json.loads(data.decode(), parse_float=float)
rates_from_rdata = rdata.get('rates', {})
for rate_symbol in ['USD', 'GBP', 'HKD', 'AUD', 'JPY', 'SEK', 'NOK']:
try:
rates.append(rates_from_rdata[rate_symbol])
with open('usio.json', 'w') as outfile:
json.dump(rdata, outfile)
history_currency = json.load(open('usio.json'))
df = pd.read_json(open('usio.json'))
df
conn = sqlite3.connect('usio.db')
df.to_sql('usio', conn, if_exists='replace')
except KeyError:
logging.warning('rate for {} not found in rdata'.format(rate_symbol))
pass
return rates
这是我的架构文件:
drop table if exists rates;
create table rates (
id integer primary key autoincrement,
currency text,
rate real
);
这段代码读取一个名为usio.json
的json文件,然后通过使用to_sql
pandas函数,将其存储到名为usio.sql
的SQLite db中,然而,它似乎工作得很好,但我的疑问或问题是,我在哪里可以检查结果
我的意思是我没有看到任何创建的文件,可能是因为我没有将SQLite数据库初始化到我的Flask应用程序中
或者我忘了指定其他内容?您需要有一个单独的方法来为您检查数据库
def printNewTable():
db = dqsqlite3.connect('usio.db')
for row in db.execute('select * from rates'):
print row
printNewTable()
您是否尝试过
db=sqlite3.connect('usio.db')
和db.execute('select*from rates')
?不是execute命令,但我的观点是,是否会因此产生一些新文件?我不这么认为。您正在专门向该数据库写入数据。如果你需要的话,你可以把数据库复制到本地并在那里进行测试。谢谢,我只是想检查结果,根本无法检查,即使在这之后,也许我真的需要把它打印到某个结果文件中?无论如何,我会继续努力,如果需要的话,问一个新问题,再次谢谢你。@NeoVe桌子上还没有任何东西。也许可以查看是否首先使用从my_db.sqlite_master中选择名称创建表,其中type='table'代码>