我对所提到的一切都是新手。我的目标是从YQL获取一些财务数据并插入MySQL,这是通过进行python调用来完成的。因此,请关注.py文件:
import mysql.connector
import yahoo.yql
cnx = mysql.connector.connect(user='conn', password='abc123', database='yahoodata')
cursorA = cnx.cursor()
cursorB = cnx.cursor()
## Firstly, read all Symbol as an array in python
myquery = ("SELECT Symbol FROM yahoodata.nasdaq100list")
cursorA.execute(myquery)
myresult = cursorA.fetchall()
## Query YQL with each Symbol, literally loop 100 times
for i in myresult:
yquery = 'select Symbol,Date,High,Low from yahoo.finance.historicaldata where symbol = "%s" and startDate = "2014-01-01" and endDate = "2014-12-31"' % i
yresult = yahoo.yql.YQLQuery().execute(yquery)
## Insert Data from YQL fetch to MySQL
insert = "INSERT INTO daily_avg_2014(Symbol,Date,High,Low) VALUES (%(symbol)s, %(date)s, %(high)s, %(low)s)"
cursorB.executemany(insert, yresult)
cursor.close()
cnx.close()
YQL成功返回JSON格式的数据。但是,INSERT将继续说" mysql.connector.errors.ProgrammingError:并非所有参数都在SQL语句中使用"
当回头查看实际插入到MySQL的内容时,我发现格式不正确JSON,因为我将它们放入字符串列表/数组
>>> print yresult
{u'query': {u'count': 252, u'lang': u'en-US', u'results': {u'quote': [{u'High': u'51.68', u'Date': u'2014-12-31', u'Symbol': u'YHOO', u'Low': u'50.459999'},
正如我所理解的,YQL似乎在JSON格式中返回我需要的结果很棒,但是我滥用Python却没有把它弄好,因此无法插入到MySQL中。任何评论表示赞赏。谢谢。
答案 0 :(得分:0)
FYI。
使用pprint成功绕过该错误
yresult = yahoo.yql.YQLQuery().execute(yquery)
jsoned_yresult = pprint.PrettyPrinter(indent=2).pformat(yresult['query']['results']['quote'])