SQLAlchemy Reflection: How do I query data from specific columns?

前端 未结 1 1522
一整个雨季
一整个雨季 2020-12-21 23:16

Using SQLAlchemy reflection, how do I query for data in specific column?

testtable = Table(\'member\', Metadata, autoload=True)

def TestConnection():
    da         


        
相关标签:
1条回答
  • 2020-12-21 23:53

    Just specify the columns to select [session.query(testtable.c.password, testtable.c.username)] instead of the whole table [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!")
    
    0 讨论(0)
提交回复
热议问题