bootalchemy提供了一种通过 SQLAlchemy 执行此操作的方法。首先,在 SQLAlchemy 模型中定义您的模式。然后使用 bootalchemy 将 YAML 加载到 SQLAlchemy 会话中。最后,对该会话执行查询。(您不必将会话提交到实际数据库。)
PyPI 页面中的示例(假设model
已定义):
from bootalchemy.loader import Loader
# (this simulates loading the data from YAML)
data = [{'Genre':[{'name': "action",
'description':'Car chases, guns and violence.'
}
]
}
]
# load YAML data into session using pre-defined model
loader = Loader(model)
loader.from_list(session, data)
# query SQLAlchemy session
genres = session.query(Genre).all()
# print results
print [(genre.name, genre.description) for genre in genres]
输出:
[('action', 'Car chases, guns and violence.')]