如何围绕现有数据库构建烧瓶应用程序?
我已经有一个现有的数据库,它在 MySQL
中有很多表和大量数据.我打算创建一个 Flask
应用程序并与它一起使用 sqlalchemy.现在我在 irc 上询问并在谷歌上环顾四周并尝试了以下想法:
I already have an existing Database that has a lot of tables and a lot of data in MySQL
. I intend to create a Flask
app and use sqlalchemy along with it. Now I asked out on irc and looked around on google and tried the following ideas:
首先我使用sqlacodegen从我的<代码>数据库代码>.但后来我对此有点困惑,又看了一些.我发现 this.
First I used sqlacodegen to generate the models from my DB
. But then I was confused about it a little and looked some more. And I found this.
这看起来是一个优雅的解决方案.
This looked like an elegant solution.
所以第二,我根据那里的解决方案重写了我的models.py
,现在我更加困惑了.我正在寻找构建此 Flask 应用程序以及现有数据库的最佳方法.
So Second, I rewrote my models.py
according to the solution there and now I am even more confused. I am looking for the best approach to build this flask app along with the already existing DB.
我查看了flask文档,但对于已经存在数据库的项目并没有真正得到任何帮助.从头开始创建东西,创建数据库和所有东西有很多好东西.但我真的很困惑.
I looked into the flask documentation but didnt really get any help for a project with an already existing db. There is a lot of good stuff for creating something from scratch, creating the db and all. But I am really confused.
请注意,这是我使用 Flask
的第一天,但我有使用 Django
的经验,因此基本概念不是障碍.我需要一些指导来为这个用例选择最佳方法.详细的解释将不胜感激.详细地说,我绝对不希望有人写出所有的代码并用勺子喂我,但这足以让我开始,那就是通过 sqlalchemy
将此数据库无缝集成到 flask
中代码>.注意我的数据库在 MySQL
中.
Please Note that its my first day with Flask
, but I have experience with Django
, so the basic concepts are not a hurdle. I need some guidance in choosing the best approach for this usecase. A detailed explanation would be greatly appreciated. By detailed I definitely do not expect someone to write all the code and spoon feed me on this, but just enough to get me started, that is integrate this db seamlessly into flask
via sqlalchemy
. Note my DB is in MySQL
.
推荐答案
我想说你的问题与 Flask 完全没有关系.例如,模板、路由、视图或登录装饰器没有问题.
I'd say your question has nothing to do with flask at all. For example, you don't have a problem with the templates, routes, views or logon decorators.
您在 SQLAlchemy 苦苦挣扎的地方.
Where you struggle at is at SQLAlchemy.
所以我的建议是暂时忽略 Flask 并先习惯 SQLAlchemy.您需要习惯现有的数据库以及如何从 SQLAlchemy 访问它.使用一些 MySQL 文档工具来解决这个问题.以这样的事情开始(注意它与 Flask ask all ... 无关):
So my suggestion is to ignore Flask for a while and get used to SQLAlchemy first. You need to get used to your existing database and how to access it from SQLAlchemy. Use some MySQL documentation tool to find your way around this. The start with something like this (note that it has nothing to do with Flask ask all ... yet):
#!/usr/bin/python
# -*- mode: python -*-
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///webmgmt.db', convert_unicode=True, echo=False)
Base = declarative_base()
Base.metadata.reflect(engine)
from sqlalchemy.orm import relationship, backref
class Users(Base):
__table__ = Base.metadata.tables['users']
if __name__ == '__main__':
from sqlalchemy.orm import scoped_session, sessionmaker, Query
db_session = scoped_session(sessionmaker(bind=engine))
for item in db_session.query(Users.id, Users.name):
print item
在engine =
"行中,您需要提供您的 MySQL 数据库的路径,以便 SQLAlchemy 找到它.在我的例子中,我使用了一个预先存在的 sqlite3 数据库.
In the line "engine =
" you need to provide your path to your MySQL database, so that SQLAlchemy finds it. In my case I used a pre-existing sqlite3 database.
在class Users(Base)
"行中,您需要使用 MySQL 数据库中的现有表之一.我知道我的 sqlite3 数据库有一个名为users"的表.
In the line "class Users(Base)
" you need to use one of existing tables in your MySQL database. I knew that my sqlite3 database had a table named "users".
在这一点之后,SQLalchemy 知道如何连接到您的 MySQL 数据库并且它知道其中一个表.您现在需要添加您关心的所有其他表.最后,您需要指定与 SQLalchemy 的关系.这里我指的是一对一、一对多、多对多、亲子等.SQLAlchemy 网站包含有关此内容的相当长的部分.
After this point, SQLalchemy knows how to connect to your MySQL database and it knows about one of the tables. You need now to add all the other tables that you care for. Finally, you need to specify relationships to SQLalchemy. Here I mean things like one-to-one, one-to-many, many-to-many, parent-child and so on. The SQLAlchemy web site contains a rather lenghty section about this.
在if __name__ == '__main__'
"这一行之后是一些测试代码.如果我不导入 python 脚本,而是运行它,它将被执行.在这里您可以看到我创建了一个数据库会话,并且是用于非常简单的查询.
After the line "if __name__ == '__main__'
" just comes some test code. It will be executed if I don't import my python script, but run. Here you see that I create a DB session and is that for a very simply query.
我的建议是,您首先阅读 SQLAlchemy 文档的重要部分,例如描述性表定义、关系模型以及如何查询.一旦你知道了这一点,你就可以将我示例的最后一部分更改为一个控制器(例如,使用 Python 的 yield
方法)并编写一个使用该控制器的视图.
My suggestion is that you first read about the important parts of SQLAlchemy's documentation, for example the descriptive table definition, the relationship model and how to query. Once you know this, you can change the last part of my example into a controller (e.g. using Python's yield
method) and write a view that uses that controller.
相关文章