In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.