本文介绍了为什么使用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 连接到它
I'm 95% positive that the filename isn't the issue as I can connect to it from the shell
有什么想法吗?
推荐答案
sessionmaker()
的返回值 是一个类.在实例上使用方法之前,您需要对其进行实例化.
The return value from sessionmaker()
is a class. You need to instantiate it before using methods on the instance.
这篇关于为什么使用sqlite在sqlalchemy中未绑定会话方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!