gpt4 book ai didi

python - SQLAlchemy.exc.UnboundExecutionError : Could not locate a bind configured on mapper Mapper|SellsTable|sellers or this Session 错误

转载 作者:太空狗 更新时间:2023-10-29 20:50:05 29 4
gpt4 key购买 nike

我创建了一个使用 SQLAlchemy 的类:

class DbAbsLayer(object):
def __init__(self):
self.setConnectionURI();
def setConnectionURI(self):
self.dbDriver = "mysql";
self.dbHostname = "localhost";
self.dbUsername = "root";
self.dbPassword = "123";
self.dbName = "mydbname";
def createSession(self):
Session = sessionmaker();
self.session = Session.configure();
self.session = Session();
def createEngine(self):
self.setConnectionURI();
self.engine = create_engine(self.dbDriver + "://" + self.dbUsername + ":" + self.dbPassword + "@" + self.dbHostname + "/" + self.dbName);

然后我创建了我的表类:

class SellsTable(declarative_base()):
__tablename__ = 'sellers'; #
id = Column(Integer,primary_key = True)
name = Column(String)
name_type = Column(Integer)
address = Column(String)
telephones = Column(String)
emails = Column(String)
job_background = Column(String)
agent_first_name = Column(String)
agent_last_name = Column(String)
agent_attributes = Column(String)
agent_values = Column(String)

它的构造函数:

def __init__(self,fieldsName ,fieldsValue):
completeVariableName = list();
tmpKeysOfFieldsValue = fieldsValue.keys();
for fieldsNameCounter in range (0,len(fieldsName)):
for filedsValueCounter in range(0,len(tmpKeysOfFieldsValue)):
if fieldsName[fieldsNameCounter] == tmpKeysOfFieldsValue[filedsValueCounter]:
completeVariableName.append("self." + tmpKeysOfFieldsValue[filedsValueCounter]);
tmpVariable = completeVariableName[filedsValueCounter];
tmpValue = fieldsValue[fieldsName[fieldsNameCounter]];
exec ('%s = \"%s\"' % (tmpVariable, tmpValue));

我有以下添加记录的函数:

def addRecord(self,tableObj):
TableClass = tableObj.__class__ ;
sessionObj = TableClass(['name','telephones'],dict([('name','golrang'),('telephones','092127878,21078288')]));
self.session.add(sessionObj); ##### MY ERROR LINE
self.session.commit();

但是当我运行 add func 时,我得到以下回溯:

Traceback (most recent call last):
File "./main.py", line 218, in <module>
main()
File "./main.py", line 210, in main
dbObj.addRecord(ddd);
File "./main.py", line 104, in addRecord
self.session.commit();
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 721, in commit
self.transaction.commit()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 354, in commit
self._prepare_impl()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 334, in _prepare_impl
self.session.flush()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1818, in flush
self._flush(objects)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1936, in _flush
transaction.rollback(_capture_exception=True)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 58, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1900, in _flush
flush_context.execute()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 372, in execute
rec.execute(self)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 525, in execute
uow
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 45, in save_obj
uowtransaction)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 140, in _organize_states_for_save
states):
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 772, in _connections_for_states
base_mapper)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 211, in connection
bind = self.session.get_bind(bindkey, **kwargs)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1102, in get_bind
', '.join(context)))
sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|SellsTable|sellers or this Session

最佳答案

您应该将引擎绑定(bind)到您的模型。

class DbAbsLayer(object):

def createSession(self):
Session = sessionmaker()
self.session = Session.configure(bind=self.engine)

关于python - SQLAlchemy.exc.UnboundExecutionError : Could not locate a bind configured on mapper Mapper|SellsTable|sellers or this Session 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18445772/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com