【发布时间】:2015-12-15 04:40:15
【问题描述】:
我想将批量数据存储到 postgresql。 我得到的数据来自谷歌分析 [API]。数据是关于浏览量的,这是我的代码:
data = '[["20151201","path","title",345], ["20151202","path","title",321], ["20151203","path","title",214]]'
def storeJson( jsonFile, tableName ):
conn = psycopg2.connect( host=hostname, user=username, password=password, dbname=database )
try:
cur = conn.cursor()
# Here is the problem:
cur.executemany( "INSERT INTO " + tableName + " VALUES(%s)", [jsonFile])
conn.commit()
except psycopg2.DatabaseError as e:
if conn:
conn.rollback()
print("Error %s" %e)
exit()
finally:
if conn:
cur.close()
conn.close()
def main()
storeJson(data, "daily_pageviews")
if __name__ == '__main__':
main()
使用上面的代码,我收到如下错误消息:
json.decoder.JSONDecodeError: Expecting ':' delimiter: line 1 column 12 (char 11)
有人可以启发我吗?谢谢大家!
【问题讨论】:
-
你能在这里添加你的
create table命令吗?需要插入数据为json还是在插入前解析? -
我认为表没有问题,因为错误与数据库无关。这是我的创建表查询:create table daily_pageviews("date" date, path text, title text, pageviews int)
标签: python postgresql google-analytics google-analytics-api