【问题标题】:SQLAlchemy Reflection: How do I query data from specific columns?SQLAlchemy 反射:如何从特定列查询数据?
【发布时间】:2013-03-06 16:21:55
【问题描述】:

使用 SQLAlchemy 反射,如何查询特定列中的数据?

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

def TestConnection():
    data = None
    loopCounter = 0 
    for data in session.query(testtable).filter_by(is_active=1, is_deleted=0): 
        print(loopCounter + 1, data)
        loopCounter += 1
    if data is None:
        raise Exception ("Could not find any data that matches your query")        
    else:
        print("It worked!")

TestConnection()

以上查询为我提供了 members 表中所有列中的所有数据。但是,我想要的是从列中获取特定数据。例如。我想检索 usernamepassword 列,但我无法正确获取语法。以下是我目前所拥有的:

def TestConnection():
    loopCounter = 0 
    for password, username in session.query(testtable).filter_by(is_active=1, is_deleted=0):
        print(loopCounter + 1, data)
        loopCounter += 1
    if data is None:
        raise Exception ("Could not find any data that matches your query")        
    else:
        print("It worked!")

失败并出现错误:

Traceback (most recent call last):
File "/home/workspace/upark/src/monitor.py", line 36, in <module>
TestConnection()
File "/home/workspace/upark/src/monitor.py", line 26, in TestConnection
for password, username in session.query(testtable).filter_by(is_active=1, is_deleted=0):
ValueError: too many values to unpack (expected 2)

我正在使用来自 Oracle 的 Python3.2、SQLAchemy0.8 和 mysqlconnector。

编辑:一些小进步

刚刚发现我可以在所有结果返回后“过滤”出列,如下所示:

def TestConnection():
    data = None
    loopCounter = 0 
    for data in session.query(testtable).filter_by(is_active=1, is_deleted=0): 
        print(loopCounter + 1, data.password, data.username)
        loopCounter += 1
    if data is None:
        raise Exception ("Could not find any data that matches your query")        
    else:
        print("It worked!")

这将给出:

1 pass1 userone
2 pass2 usertwo

但是正如你所看到的,那是在我把所有的列都拿回来之后。我想要的是只从我需要的列中获取数据。例如。成员表有 10 列。为了提高效率,我只需要从其中两个中获取数据。

【问题讨论】:

    标签: python mysql sqlalchemy


    【解决方案1】:

    只需指定要选择的列 [session.query(testtable.c.password, testtable.c.username)] 而不是整个表 [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!")
    

    【讨论】:

    • 那不行。它失败并出现错误for data in session.query(testtable.password, testtable.username).filter_by(is_active=1, is_deleted=0): AttributeError: 'Table' object has no attribute 'password' 请记住我正在使用反射,所以我没有在 python 中自己创建“列”
    • 更新了答案 - 需要使用 testtable.c.column1 而不是 testtable.column1
    • 尝试了你的建议,但它现在抛出了一个不同的错误:File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/inspection.py", line 74, in inspect type_) sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type &lt;class 'NoneType'&gt;
    • 尝试使用 filter 而不是 filter_by 使用限定的列名:qry = session.query(testtable.c.password, testtable.c.username).filter(testtable.c.is_active==1, testtable.c.is_deleted==0)
    • 终于成功了。谢谢。两件事情。一、为什么使用“filter_by”时会出现non type错误?二,你有没有一个指针可以让我更好地解释 SQLAlchemy 反射?因为我怀疑我在这里问的是 sqlalchemy 的文档页面,如果是的话,它就没有那么直接了。
    猜你喜欢
    • 2019-05-29
    • 2013-02-24
    • 2018-01-23
    • 1970-01-01
    • 2022-09-25
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多