0

在这个项目中,我试图保存 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()
    }
4

1 回答 1

1

我希望这应该是我最后一次编辑。如果你有一个漫长的忙碌的一周,那么周五就会变得毫无成效。:) 好的,这就是设置的方式,它对我有用:

//Domain class StockQuote
class StockQuote {
    String ticker
    String stockInfo

    static mapping = {
        stockInfo type: "text" //You have to set the type since the info is CLOB
    }
}

//Controller Action
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) //Look at how flush is used in save
    def stockQuoteList = stockQuoteList()   
    render template: 'usedTickers', collection: stockQuoteList, var: 'stockData'
}

以上对我有用,我能够将来自 url 的大 CLOB 响应保存到表中。看看我的测试

于 2013-05-31T17:47:22.930 回答