使用我当前的代码,我能够返回包含来自给定表(服务器,端口,用户和环境)的所有数据的字典,但是我怎么才能仅返回,例如,仅返回用户和环境?
engine_str = con_string.format(
login=login, passwd=pwd, hostname=hostname, port=db_port, db=db_name
)
try:
engine = sqlalchemy.create_engine(engine_str, echo=False)
session = sessionmaker(bind=engine)
connection = engine.connect()
session = session(bind=connection)
Base = declarative_base()
except exc.SQLAlchemyError:
raise
def select_data(server, trigger_name):
_t = Table('trigger_details', Base.metadata, autoload_with=engine)
try:
stm = session.query(_t).filter_by(
server=server, trigger_name=trigger_name
)
column = [c["name"] for c in stm.column_descriptions]
return [dict(zip(column, row)) for row in stm.all()]
finally:
session.close()
答案 0 :(得分:0)
解决:
engine_str = con_string.format(
login=login, passwd=pwd, hostname=hostname, port=db_port, db=db_name
)
try:
engine = sqlalchemy.create_engine(engine_str, echo=False)
session = sessionmaker(bind=engine)
connection = engine.connect()
session = session(bind=connection)
Base = declarative_base()
except exc.SQLAlchemyError:
raise
def select_data(server, trigger_name):
_t = Table('trigger_details', Base.metadata, autoload_with=engine)
try:
stm = session.query(_t.c.user, _t.c.environment).filter(
server == server, trigger_name == trigger_name
)
column = [c["name"] for c in stm.column_descriptions]
return [dict(zip(column, row)) for row in stm.all()]
finally:
session.close()