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

aar*_*ing 7 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()
Run Code Online (Sandbox Code Playgroud)

运行此代码会产生:

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)
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗?

Ign*_*ams 12

返回值sessionmaker() 是一个类.您需要在实例上使用方法之前对其进行实例化.