为什么会话方法在sqlalchemy中使用sqlite解除绑定?

时间:2010-07-11 04:01:54

标签: python sqlite sqlalchemy

代码复制错误:

from sqlalchemy import create_engine, Table, Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class Message(Base):
    __tablename__ = 'messages'

    id = Column(Integer, primary_key=True)
    message = Column(Integer)


engine = create_engine('sqlite:///' + filename_of_your_choice)
session = sessionmaker(bind=engine)

newmessage = Message()
newmessage.message = "Hello"

messages = session.query(Message).all()

运行此代码会产生:

Traceback (most recent call last):
  File "C:/aaron/test.py", line 20, in <module>
    session.commit()
TypeError: unbound method commit() must be called with Session instance as first argument (got nothing instead)

我95%肯定文件名不是问题,因为我可以从shell连接到它

任何想法?

1 个答案:

答案 0 :(得分:12)

sessionmaker() is a class的返回值。您需要在实例上使用方法之前对其进行实例化。