Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to operate Flask-4 database using SQLAlchemy

Shulou Source: shulou.com Published: 2022-06-02 00:44:12 10月01日 Update

In this issue, the editor will bring you about how to operate Flask-4 database and use SQLAlchemy. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

1. What is Flask-SQLAlchemy?

SQLAlchemy is an open source software under the Python programming language. The SQL toolkit and object-relational mapping (ORM) tools are provided.

two。 How to use Flask-SQLAlchemy?

Execute pip install flask-sqlalchemy from the command line to install:

Continue with the previous chapter, modify the flaskblog.py in the Flask_Blog folder (the modified section has been marked as shown in the figure):

Using sqlalchemy, open the cmd command line under the Flask_Blog folder, enter python, and import the db instance:

Next, use the db instance to demonstrate some database operations:

# perform database site.db creation, initialization creation of user table and post table > db.create_all () # We will see a site.db file generated under the Flask_Blog folder

# Import entity class objects User and Post > from flaskblog import User,Post# initialize user_1 objects and assign values > user_1 = User (username='Tbag',email='Tbag@email.com',password='password') # Save user_1 to database > db.session.add (user_1) # # initialize user_2 objects and assign values > user_2 = User (username='Yale',email='Yale@email.com' Password='password2') # Save user_2 to database > db.session.add (user_2) # commit transaction > db.session.commit () # query all user information > User.query.all () [User ('Tbag','Tbag@email.com','default.jpg'), User (' Yale','Yale@email.com','default.jpg')] # query the first data > User.query.first () User ('Tbag','Tbag@email.com') 'default.jpg') # query by condition > User.query.filter_by (username='Yale'). All () [User (' Yale','Yale@email.com','default.jpg')] > User.query.filter_by (username='Yale'). First () User ('Yale','Yale@email.com','default.jpg') # query results are assigned to user objects > user = User.query.filter_by (username='Yale'). First () > > userUser (' Yale') 'Yale@email.com','default.jpg') # get user id > user.id2# query users according to the main key id > user = User.query.get (2) > userUser (' Yale','Yale@email.com','default.jpg') # get users' blogs: empty > user.posts [] # initialize two pieces of blog data # add to the database > post_1 = Post (title='Blog 1) User_id=user.id) > post_2 = Post (title='Blog 2) contentless second blog',user_id=user.id) > > db.session.add (post_1) > db.session.add (post_2) > db.session.commit () # get the user's blog information again > user.posts [Post ('Blog 1,' 2019-06-17 12 18 Vane 20.586947'), Post ('Blog 2') The above is how to use SQLAlchemy in the Flask-4 database shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Data databases objects queries files users content blogs folders information commands instances tools analysis next professional small and medium transactions rich content again Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Shulou Technology OPPO Reno NVidia