2015-09-03 17 views
2

我要實現我的utils的一個簡單的流程:用SQLAchemy更新數據集的Python方法是什麼?

  1. 從數據庫獲取rs=Query()
  2. 遍歷rs併發送請求requests.get(rs['http'])
  3. 解析響應,並得到logo_url
  4. 更新rs數據集
import requests 

from sqlalchemy import create_engine 

engine = create_engine("mssql://me:[email protected]/db") 

md = sqlalchemy.MetaData(engine) 
table = sqlalchemy.Table('stations', md, autoload=True) 

station = engine.execute("select * from stations") 
for row in station: 
    upd = table.filter_by(id=5).update(values={'STATION_LOGO_URL': 'http://test.com/img.png'}) 
    engine.execute(upd) 

是否有可能用SQLAlchemy做這樣的事情?

回答

2

我不知道沒有使用SQLAlchemy ORM的方法。但與ORM它很簡單:

class Station(Base): 
    __tablename__ = 'stations' 
    ... 
    station_logo_url = Column('STATION_LOGO_URL', NVARCHAR) 
    ... 

... 

stations = session.query(Station).all() 
for station in stations: 
    logo_url = get_logo_url(station) 
    station.station_logo_url = logo_url 
session.commit() 
+0

你是什麼意思下的「ORM」? – SpanishBoy

+0

@SpanishBoy對象關係映射 –

+0

我明白了:)但是我問的是ORM使用什麼框架而不是SQLAlchemy ORM? – SpanishBoy

相關問題