Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 17th, 2012  |  syntax: None  |  size: 2.56 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. SQLAlchemy: Problems Migrating to PostgreSQL from SQLite (e.g. sqlalchemy.exc.ProgrammingError:)
  2. raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect)
  3.  
  4. sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt 'INSERT INTO cnn_hot_stocks (datetime, list, ticker, price, change, "pctChange") VALUES (%(datetime)s, %(list)s, %(ticker)s, %(price)s, %(change)s, %(pctChange)s)' {'price': Decimal('7.94'), 'list': 'active', 'datetime': datetime.datetime(2012, 6, 23, 11, 45, 1, 544361), 'pctChange': u'+1.53%', 'ticker': u'BAC', 'change': Decimal('0.12')}
  5.        
  6. dbstring = "postgresql://postgres:postgres@localhost:5432/algo"
  7. db = create_engine(dbstring)
  8. db.echo = True  # Try changing this to True and see what happens
  9. metadata = MetaData(db)
  10.  
  11. cnn_hot_stocks = Table('cnn_hot_stocks', metadata, autoload=True)
  12.  
  13. i = cnn_hot_stocks.insert() # running log from cnn hot stocks web-site
  14.  
  15. def scrape_data():
  16.     try:
  17.             html = urllib2.urlopen('http://money.cnn.com/data/hotstocks/').read()
  18.             markup, errors = tidy_document(html)
  19.             soup = BeautifulSoup(markup,)
  20.     except Exception as e:
  21.             pass
  22.     list_map = { 2 : 'active',
  23.                  3 : 'gainer',
  24.                  4 : 'loser'
  25.                }
  26.     # Iterate over 3 tables on CNN hot stock web-site
  27.     for x in range(2, 5):
  28.             table = soup('table')[x]
  29.             for row in table.findAll('tr')[1:]:
  30.                     timestamp = datetime.now()
  31.                     col = row.findAll('td')
  32.                     ticker = col[0].a.string
  33.                     price = Decimal(col[1].span.string)
  34.                     change = Decimal(col[2].span.span.string)
  35.                     pctChange = col[3].span.span.string
  36.                     log_data = {'datetime'  : timestamp,
  37.                                 'list'      : list_map[x],
  38.                                 'ticker'    : ticker,
  39.                                 'price'     : price,
  40.                                 'change'    : change,
  41.                                 'pctChange' : pctChange
  42.                                }
  43.                     print log_data
  44.                     # Commit to DB
  45.                     i.execute(log_data)
  46.        
  47. cnn_hot_stocks = Table('cnn_hot_stocks', metadata, # log of stocks data on cnn hot stocks lists
  48.             Column('datetime', DateTime, primary_key=True),
  49.             Column('list', String), # loser/gainer/active
  50.             Column('ticker', String),
  51.             Column('price', Numeric),
  52.             Column('change', Numeric),
  53.             Column('pctChange', String),
  54.             )