11

を操作するためのクラスを作成します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);

次に、My テーブル クラスを作成しました。

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));

を追加するための次の機能がありますrecord

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
4

1 に答える 1

19

エンジンをモデルにバインドする必要があります。

class DbAbsLayer(object):

    def createSession(self):
        Session = sessionmaker()
        self.session = Session.configure(bind=self.engine)
于 2013-08-26T15:02:59.060 に答える