来自API调用的SQLite

时间:2016-06-20 14:31:59

标签: python sqlite api

我试图通过进行API调用并存储到对象调用"数据"来从quandl站点插入几千行。 数据的结构只是日期和价格

import quandl as q
import sqlite3 as sq

token = "asdaasdewqrdfc"

data = quandl.get("WGC/GOLD_DAILY_USD", authtoken=token)

conn = sq.connect("RVDB.db")

c = conn.cursor()
c.execute("CREATE TABLE Gold(PricingDate DATE, Price FLOAT)")
c.execute("INSERT INTO Gold VALUES('??','??')")

conn.commit()

有什么想法吗?

输出output

1 个答案:

答案 0 :(得分:0)

quandl.get返回的是Pandas数据帧。使用Pandas实用程序函数.to_sql()插入数据:

import quandl as q
import sqlite3 as sq

token = "asdaasdewqrdfc"
token = None

data = q.get("WGC/GOLD_DAILY_USD", authtoken=token)

conn = sq.connect("RVDB.db")
data.to_sql('Gold', conn, if_exists='replace',
            dtype={'Date':'DATE', 'Price':'FLOAT'})
conn.commit()