python - 通过 pandas to_sql 命令检查数据库插入结果 - Flask

标签 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(), 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 的 SQLite 数据库中。 sql,但是,它似乎工作得很好,但我的疑问或问题是,我在哪里可以检查结果?

我的意思是我没有看到任何文件被创建,这可能是因为我还没有将 SQLite 数据库初始化到我的 Flask 应用程序中吗?

或者也许我忘记指定其他内容?

最佳答案

您需要有一个单独的方法来为您进行数据库检查。

    def printNewTable():
        db = dqsqlite3.connect('usio.db')
        for row in db.execute('select * from rates'):
            print row
    printNewTable()

关于python - 通过 pandas to_sql 命令检查数据库插入结果 - Flask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52032149/

相关文章:

python - 使平面适合 3D 中的一组点 : scipy. optimize.minimize vs scipy.linalg.lstsq

python - 用于查找字符串中超过 4 个连续重复字符的正则表达式

python - Pandas :具有多个索引的滚动总和(即面板数据)

sqlite - LIKE 语句可以优化成不做全表扫描吗?

python - 创建规则间隔的数字序列

python - Django 休息框架只创建序列化程序字段

python - Pandas read_excel 返回 'not enough values to unpack (expected 2, got 1)'

python - 解释 Pandas 列引用语法

android同步sql数据库访问

c++ - OpenFileDialog->DialogShow() 结果导致 SQLite 出错