Flask-SQLAlchemy is fun to use, incredibly easy for basic applications, andreadily extends for larger applications. For the complete guide, checkoutthe API documentation on the SQLAlchemy
class.
DB is included in WeChat OCR engine; DB is included in OpenCV; DB is included in PaddleOCR; Introduction. This is a PyToch implementation of 'Real-time Scene Text Detection with Differentiable Binarization'. Real-time Scene Text Detection with Differentiable Binarization. Note: some code is inherited from MhLiao/DB. 2020-06-07: 添加灰度图训练. Inmagic DB/TextWorks combines a database management system with powerful search capabilities, and can be customized and managed by non-IT staff to organize information and share knowledge throughout an organization. DB/TextWorks is offered on-premises.
A Minimal Application¶
For the common case of having one Flask application all you have to do isto create your Flask application, load the configuration of choice andthen create the SQLAlchemy
object by passing it the application.
Once created, that object then contains all the functions and helpersfrom both sqlalchemy
and sqlalchemy.orm
. Furthermore itprovides a class called Model
that is a declarative base which can beused to declare models:
To create the initial database, just import the db
object from aninteractive Python shell and run theSQLAlchemy.create_all()
method to create thetables and database:
Boom, and there is your database. Now to create some users:
But they are not yet in the database, so let’s make sure they are:
Accessing the data in database is easy as a pie:
Note how we never defined a __init__
method on the User
class?That’s because SQLAlchemy adds an implicit constructor to all modelclasses which accepts keyword arguments for all its columns andrelationships. If you decide to override the constructor for anyreason, make sure to keep accepting **kwargs
and call the superconstructor with those **kwargs
to preserve this behavior:
Simple Relationships¶
SQLAlchemy connects to relational databases and what relational databasesare really good at are relations. As such, we shall have an example of anapplication that uses two tables that have a relationship to each other:
First let’s create some objects:
As you can see, there is no need to add the Post
objects to thesession. Since the Category
is part of the session all objectsassociated with it through relationships will be added too. It doesnot matter whether db.session.add()
is called before or after creating these objects. The association canalso be done on either side of the relationship - so a post can becreated with a category or it can be added to the list of posts ofthe category.
Let’s look at the posts. Accessing them will load them from the databasesince the relationship is lazy-loaded, but you will probably not noticethe difference - loading a list is quite fast:
While lazy-loading a relationship is fast, it can easily become a majorbottleneck when you end up triggering extra queries in a loop for morethan a few objects. For this case, SQLAlchemy lets you override theloading strategy on the query level. If you wanted a single query toload all categories and their posts, you could do it like this:
Db Text Software
If you want to get a query object for that relationship, you can do sousing with_parent()
. Let’s excludethat post about Snakes for example:
Road to Enlightenment¶
The only things you need to know compared to plain SQLAlchemy are:
Dbtext Access
Dbtext Inmagic
SQLAlchemy
gives you access to the following things:all the functions and classes from
sqlalchemy
andsqlalchemy.orm
a preconfigured scoped session called
session
the
metadata
the
engine
a
SQLAlchemy.create_all()
andSQLAlchemy.drop_all()
methods to create and drop tables according to the models.a
Model
baseclass that is a configured declarative base.
The
Model
declarative base class behaves like a regularPython class but has aquery
attribute attached that can be used toquery the model. (Model
andBaseQuery
)You have to commit the session, but you don’t have to remove it atthe end of the request, Flask-SQLAlchemy does that for you.