In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Use of database operations
Sqlalchemy module installation (need to install MySQL-python/python-devel) pip install flask-sqlalchemy
Directory structure
├── app │ ├── _ _ init__.py │ └── models.py ├── config.py
Configure the environment
Vim config.py#coding:utf-8SQLALCHEMY_DATABASE_URI = 'mysql://mysqlTest:mysqlTest@192.168.3.85/mysqlTest'SQLALCHEMY_TRACK_MODIFICATIONS = True # python version underreports warning # initialization vim app/__init__.pyfrom flask import Flaskfrom flask_sqlalchemy import SQLAlchemyapp = Flask (_ _ name__) app.config.from_object (' config') db = SQLAlchemy (app) from app import models# definition table model (for example) vim app/models .pyfrom app import dbclass User (db.Model): id = db.Column (db.Integer) Primary_key = True) nickname = db.Column (db.String (64), index=True,unique = True) email = db.Column (db.String, index=True,unique = True) def _ repr__ (self): return''% (self.nickname)
Introduction to the use of shell mode
# create data model / delete data model > > from app import db > from app.models import User # Import User model > db.create_all () # create database model > db.drop_all () > db.create_all () # add data # submission method of individual data > name_admin = User (nickname='admin' Email='admin@flask.com') # assignment data > db.session.add (name_admin) # add data > db.session.commit () # submission method of more than one data > name_user1 = User (nickname='user1',email='user1@flask.com') > name_user2 = User (nickname='user2',email='user2@flask.com') > db.session.add_all ([name_user1] Name_user2]) # submit a list > db.session.commit () mysql > select * from user +-+ | id | nickname | email | +-+ | 1 | admin | admin@flask.com | | 2 | user1 | user1@flask.com | | 3 | user2 | user2@flask.com | + -+
Query data
# query all data all=User.query.all () # filter query (filter_by ()), query rows of nickname=user1 > user1 = User.query.filter_by (id=2). First () > > user2 = User.query.filter_by (id=3). First () > > user1 > user2# query the fields of this data Check the value of the nickname field > user1.nicknameu'user1' > user2.nicknameu'user2'# result display limit (limit ()), and display two query results > result = User.query.limit (2). All () > result [,]
Modify data
# modify the data, modify the value of the corresponding field and submit it directly # modify the value of the nickname field of use1 to user001user1.nickname = 'user001'db.session.add (user1) db.session.commit () delete data db.session.delete (user1) db.session.commit ()
# Common query filter filter () adds a filter to the original query, returns a new query filter_by (), adds an equivalent filter to the original query, returns a new query limit (), limits the number of results returned by the original query with the specified value, and returns a new query offset () that offsets the results returned by the original query. Returns a new query order_by () sorts the original query results according to the specified criteria, returns a new query group_by () groups the original query results according to the specified conditions, and returns a new query # Common query execution function all () returns all the results of the query in the form of a list first () returns the first result of the query, if there is no result Then Nonefirst_or_404 () returns the first result of the query. If there is no result, the request is terminated. The error response get () returns the row corresponding to the specified primary key. If there is no corresponding row, Noneget_or_404 () returns the row corresponding to the specified primary key. If the specified primary key is not found, the request is terminated. Return 404 error response count () returns the number of query results paginate () returns a Paginate object that contains the results within the specified range
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.