스크래피와 함께 일하고 있어요. 문서 ( http://docs.sqlalchemy.org/en/latest/orm/session_basics.html#getting-a-session ) 에 따라 'contacts'라는 테이블이있는 테이블에 대한 sqlalchemy 세션에 액세스하고 싶습니다. 다음을 만들었습니다.
engine = create_engine('sqlite:///data.db')
# create a configured "Session" class
Session = sessionmaker(bind=engine)
# create a Session
session = Session()
class ContactSpider(Spider):
.......
def parse(self, response):
print('hello')
session.query(contacts).filter_by(name='ed').all()
그러나 기존 테이블에 연결하는 방법이 보이지 않습니다. 어떻게하나요?
리플렉션을 통해 기존 테이블에 연결할 수 있습니다. 불행히도 귀하의 질문에는 일부 코드 설정이 없으므로 아래는 일반적인 의사 코드 예입니다 (테이블 이름이라고 가정 contacts
)
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# Look up the existing tables from database
Base.metadata.reflect(engine)
# Create class that maps via ORM to the database table
Contact = type('Contact', (Base,), {'__tablename__': 'contacts'})
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다