Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

SQLAlchemy Reflection:Como faço para consultar dados de colunas específicas?


Basta especificar as colunas para selecionar [session.query(testtable.c.password, testtable.c.username) ] em vez de toda a tabela [session.query(testtable) ]:
def TestConnection():
    data = None
    loopCounter = 0 
    for data in session.query(testtable.c.password, testtable.c.username).filter_by(is_active=1, is_deleted=0): 
        pwd, usr = data
        print(loopCounter + 1, pwd, usr)
        loopCounter += 1
    if data is None:
        raise Exception ("Could not find any data that matches your query")        
    else:
        print("It worked!")