在这个项目中,我试图保存 Yahoo Finance URL API 返回的数据。要访问的 URL 是“ http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=0&e=31&f=2010&g=w&ignore=.csv ”。我尝试将返回的数据保存为我的 StockQuote.stockInfo 参数中的字符串。
保存 StockQuote 实例对象时出现以下错误:
Error 500: Internal Server Error
URI: /StockScreenerSD/stockQuote/tickerLookup
Class: org.h2.jdbc.JdbcSQLException
Message: NULL not allowed for column "STOCK_QUOTE"; SQL statement: insert into stock_quote (id, version, date_created, stock_info, ticker) values (null, ?, ?, ?, ?) [23502-164]
Around line 26 of grails-app/controllers/org/grails/finance/StockQuoteController.groovy
23:// def url = ("http://download.finance.yahoo.com/d/quotes.csv?s=" + stockQuote.ticker + "&f=xsna2t8pj1a").toURL()
24: def url = ("http://ichart.yahoo.com/table.csv?s=" +stockQuote.ticker+ "&a=0&b=1&c=2000&d=0&e=31&f=2010&g=w&ignore=.csv").toURL()
25: stockQuote.stockInfo = url.text.toString()
26: stockQuote.save()(flush: true)
27: def stockQuoteList = stockQuoteList()
28: render template: 'usedTickers', collection: stockQuoteList, var: 'stockData'
29: }
尝试保存操作的我的控制器代码如下:
package org.grails.finance
import grails.plugins.springsecurity.Secured
@Secured('IS_AUTHENTICATED_FULLY')
class StockQuoteController {
// def scaffold = true
def index() {
redirect (action:'getTicker')
}
def getTicker() {
def listing = stockQuoteList()
return [stockQuoteList: listing] // this is a map. key=>value
}
def tickerLookup = {
def stockQuote = new StockQuote(ticker: params.ticker)
def url = ("http://ichart.yahoo.com/table.csv?s=" +stockQuote.ticker+ "&a=0&b=1&c=2000&d=0&e=31&f=2010&g=w&ignore=.csv").toURL()
stockQuote.stockInfo = url.text.toString()
stockQuote.save()(flush: true)
def stockQuoteList = stockQuoteList()
render template: 'usedTickers', collection: stockQuoteList, var: 'stockData'
}
private stockQuoteList() {
StockQuote.list()
}