设置 Pyramid 使用 MySQL raw 而不是 SQLAlchemy
我们正在尝试建立一个使用 MySQL 而不是 SQLAlchemy 的 Pyramid 项目。
我对 Pyramid/Python 的经验有限,所以我希望能在网上找到一个指南。不幸的是,我没能找到任何东西来推动我们朝着正确的方向前进。大多数搜索结果都是针对尝试将原始 SQL/MySQL 命令与 SQLAlchemy 一起使用的人(许多搜索结果是重新发布的链接)。
有人有这方面的有用教程吗?
We're trying to set up a Pyramid project that will use MySQL instead of SQLAlchemy.
My experience with Pyramid/Python is limited, so I was hoping to find a guide online. Unfortunately, I haven't been able to find anything to push us in the right direction. Most search results were for people trying to use raw SQL/MySQL commands with SQLAlchemy (many were re-posted links).
Anyone have a useful tutorial on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
金字塔的底层并不假设您将使用任何一个特定的库来帮助您坚持下去。为了让事情变得更容易,对于那些确实希望使用 SQLALchemy 等库的人来说,Pyramid 库包含 脚手架,本质上是基本站点的一些自动生成的代码,并添加了一些用于设置 SQLAlchemy 等项目的内容或者具体的路由策略。金字塔文档应该能够引导您创建使用“pyramid_starter”脚手架的新项目,它在没有 SQLAlchemy 的情况下设置基本站点。
这将为您提供设置视图所需的基础知识,但接下来您需要添加代码以允许连接到数据库。幸运的是,由于您的网站只是 python 代码,因此学习如何在 Pyramid 中使用 MySQL 只是学习如何使用Python 中的 MySQL,然后在 Pyramid 项目中执行完全相同的步骤。
请记住,即使您更愿意使用原始 SQL 查询,您仍然可能会在 SQLAlchemy 中发现一些用处。在其基础级别,SQLAlchemy 只是封装了 DBAPI 调用并添加了连接池等有用的功能。 ORM 功能实际上是对紧密的底层 SQLAlchemy 工具集的一个重要补充。
Pyramid at its base does not assume that you will use any one specific library to help you with your persistence. In order to make things easier, then, for people who DO wish to use libraries such as SQLALchemy, the Pyramid library contains Scaffolding, which is essentially some auto-generated code for a basic site, with some additions to set up items like SQLAlchemy or a specific routing strategy. The pyramid documentation should be able to lead you through creating a new project using the "pyramid_starter" scaffolding, which sets up the basic site without SQLAlchemy.
This will give you the basics you need to set up your views, but next you will need to add code to allow you to connect to a database. Luckily, since your site is just python code, learning how to use MySQL in Pyramid is simply learning how to use MySQL in Python, and then doing the exact same steps within your Pyramid project.
Keep in mind that even if you'd rather use raw SQL queries, you might still find some usefulness in SQLAlchemy. At it's base level, SQLAlchemy simply wraps around the DBAPI calls and adds in useful features like connection pooling. The ORM functionality is actually a large addition to the tight lower-level SQLAlchemy toolset.
sqlalchemy 不会假设您将使用它的 orm。如果您希望使用纯 sql,您可以这样做,只需要 sqlalchemy 已经提供的功能即可。例如,如果您遵循食谱中的食谱,您将获得访问 sqlalchemy 会话对象
request.db
,您的处理程序将如下所示:sqlalchemy does not make any assumption that you will be using it's orm. If you wish to use plain sql, you can do so, with nothing more than what sqlalchemy already provides. For instance, if you followed the recipe in the cookbook, you would have access to the sqlalchemy session object as
request.db
, your handler would look something like this:快速教程展示了一个使用 SQL 的 Pyramid 应用程序,但不是 SQLAlchemy。它使用 SQLite,但应该相当容易适应 MySQL。
The Quick Tutorial shows a Pyramid application that uses SQL but not SQLAlchemy. It uses SQLite, but should be reasonably easy to adapt for MySQL.