Camelot a good tool for me

Lele Gaifax lele at metapensiero.it
Sun May 24 09:28:07 EDT 2015


Chris Angelico <rosuav at gmail.com> writes:

> You don't automatically get nice objects with attributes named after
> columns, but neither will SQLAlchemy unless you're doing the
> equivalent of "select * from". Example:
>
>>>> session.query(Student).all()
> [<__main__.Student object at 0x7fc2de3fc290>, <__main__.Student object
> at 0x7fc2de3fc350>, <__main__.Student object at 0x7fc2de3fc3d0>]
>
> That's a list of Student objects, because I asked for the entire table.
>
>>>> session.query(Student.name, Student.year).all()
> [(u'Fred', 2015), (u'Joe', 2015), (u"Robert'); DROP TABLE Students;--", 2012)]
>
> That's a list of tuples, because I asked for just two columns.

Wrong:

    >>> res = session.query(Student.name, Student.year).all()
    >>> type(res)
    <class 'list'>
    >>> type(res[0])
    <class 'sqlalchemy.util._collections.result'>
    >>> type(res[0].name)
    <class 'str'>
    >>> type(res[0][0])
    <class 'str'>
    >>> res[0].name == res[0][0]
    True

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele at metapensiero.it  |                 -- Fortunato Depero, 1929.




More information about the Python-list mailing list