0

In this project I am attempting to save data returned by Yahoo Finance URL API. The URL to be accessed is "http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2000&d=0&e=31&f=2010&g=w&ignore=.csv". I attempt to save the returned data as a String in my StockQuote.stockInfo parameter.

I get the following error with saving a StockQuote instance object:

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: }

My Controller Code which attempts the saving action is as below:

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()
    }
Sanket Deshpande
  • 293
  • 2
  • 3
  • 10

1 Answers1

1

I hope this should be my last edit. Proof that friday becomes unproductive if you had a long busy week. :) Ok, here is how the setup has to be, it works for me:

//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'
}

This above works for me and I am able to save the large CLOB response from the url to the table. Have a look at my test.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • i used this webpage to guide me...http://mrhaki.blogspot.co.uk/2009/10/groovy-goodness-reading-url-content.html?m=1 – Sanket Deshpande May 31 '13 at 18:17
  • Can you show `StockQuote` domain class? I think my Friday fever has already started. Before that, I wanted to provide an appropriate answer :) – dmahapatro May 31 '13 at 18:47
  • I hope I was able to address your problem Sanket. I think its high time for me to go for a vacation in India. :) – dmahapatro May 31 '13 at 19:15
  • oops, i'm not at the laptop right now. but thanks for the response! i will give it a go when i get back...thanks! – Sanket Deshpande May 31 '13 at 19:59
  • I plugged your pastebin code into a new grails project in my Eclipse GGTS environment. It works perfectly. But my own project doesn't, despite it having the same code... – Sanket Deshpande Jun 02 '13 at 19:20