如何围绕已经存在的数据库构建瓶颈应用程序? [英] How to build a flask application around an already existing database?

查看:98
本文介绍了如何围绕已经存在的数据库构建瓶颈应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经拥有一个现有的数据库,它在 MySQL 中有许多表和大量的数据。我打算创建一个 Flask 应用程序,并使用sqlalchemy。现在我问了一些关于irc的问题,并且在google上看了一下,并试着提出以下建议:

第一我用 sqlacodegen 从我的 DB 生成模型。但是当时我对此有些困惑,而且看上去更多一些。我发现



这看起来像一个优雅的解决方案。 第二,我根据那里的解决方案重写了我的 models.py ,现在我更加困惑了。我正在寻找与现有的数据库一起构建这个应用程序的最佳方法。



我查看了烧瓶文档,但是对于已有数据库的项目并没有真正的帮助。有很多好东西从头开始创建一些东西,创建数据库和所有。但是我真的很困惑。

请注意,我的第一天用 Flask ,但是我有 Django ,所以基本的概念不是一个障碍。我需要一些指导,为这个用例选择最好的方法。详细的解释将不胜感激。通过详细我绝对不会指望有人写所有的代码和勺喂我这一点,但足以让我开始,就是将这个数据库无缝地集成到 flask 通过 SQLAlchemy的。注意我的数据库在 MySQL

解决方案

问题与烧瓶无关。例如,您对模板,路由,视图或登录装饰器没有任何问题。



您在SQLAlchemy处苦苦挣扎。



所以我的建议是忽略Flask一段时间,先习惯SQLAlchemy。您需要习惯现有的数据库以及如何从SQLAlchemy访问它。使用一些MySQL文档工具来找到你的解决方法。 (注意,它与Flask没有任何关系,请问所有...):

 #! / usr / bin / python 
# - * - mode:python - * -
$ b $ from sqlalchemy import create_engine $ b $ 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导入关系,backref

class用户(Base):
__table__ = Base.metadata.tables ['users']

$ b $如果__name__ =='__main__':
from sqlalchemy.orm import scoped_session,sessionmaker,Query
db_session = scoped_session(sessionmaker(bind = engine))
db_session.query(Users.id,Users.name)中的项目:
打印项目

engine = 行中,你需要提供你的MySQL数据库的路径,这样SQLAlche我发现它。在我的情况下,我使用了一个预先存在的sqlite3数据库。



class Users(Base)您需要使用MySQL数据库中现有的一个表。我知道我的sqlite3数据库有一个名为users的表。



在此之后,SQLalchemy知道如何连接到MySQL数据库,并知道其中一个表。您现在需要添加所有您关心的其他表格。最后,您需要指定与SQLalchemy的关系。这里指的是一对一,一对多,多对多,母芯片等等。 SQLAlchemy网站包含一个相当长的部分。



如果__name__ =='__main __'行> 只是来了一些测试代码。它会被执行,如果我不导入我的Python脚本,但运行。在这里,你看到我创建了一个数据库会话,这是一个非常简单的查询。

我的建议是,你首先阅读SQLAlchemy的文档的重要部分,例如描述性表定义,关系模型以及如何查询。一旦你知道了这一点,你可以把我的例子的最后一部分改变成一个控制器(例如使用Python的 yield 方法)并编写一个使用该控制器的视图。


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:

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.

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.

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.

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.

解决方案

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.

Where you struggle at is at SQLAlchemy.

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

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.

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".

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-chip and so on. The SQLAlchemy web site contains a rather lenghty section about this.

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.

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.

这篇关于如何围绕已经存在的数据库构建瓶颈应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆