使用 SQLAlchemy 反射,如何将数据插入/更新到表中?
我刚刚了解了如何使用 SQLAlchemy Reflection 查询特定表,详见此处。完成了数据操作,现在我想保存到数据库中,但我不知道如何保存。这是我到目前为止所尝试的。
class Member(Base):
__table__ = Table('member', Metadata, autoload=True)
def set_password(self, member_id, password):
data = None
for data in session.add( self.__table__.c.member_id, self.__table__.c.parking_slot_id):
self.__table__.c.id = member_id
self.__table__.c.password = password
session.flush()
那失败了:
Traceback (most recent call last):
File "/home/workspace/testmod/src/tester.py", line 44, in <module>
m.set_password(m, 1, "password")
File "/home/workspace/testmod/src/tester.py", line 36, in set_password
for data in session.add(self.__table__.c.id, self.__table__.c.password):
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/orm/session.py", line 1363, in add
if _warn and self._warn_on_events:
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/sql/expression.py", line 1932, in __bool__
raise TypeError("Boolean value of this clause is not defined")
TypeError: Boolean value of this clause is not defined
那么,我们如何更新/插入反射表?