SQLAlchemy -orm, Flask database addition, deletion, modification, query, sqlalchemy commonly used data types, column commonly used parameters, and aggregate functions

tags: sql  python  flask

SQLAlchemy -orm

Use orm to create a table
Connect to the database
Create Base Base Class
Create classes, add attributes, and add data type constraints
Create session
Add create instance add data
Add data to the database
submit data


Adding, deleting, modifying and checking the Flask database

def add_data():
user = User (add attribute data)
session.add(user)
sesssion.commit()

def search_data():
data = session.query(Usre).filter(conditional judgment).all()
print(data)

def update_data():
data = session.query(Usre).filter(conditional judgment).all()
data.attribute name = xxx
session.commit()

def delete_data():
data = session.query(Usre).filter(conditional judgment).all()
session.rollback()
session.delete(data)

sqlalchemy common data types

		Integer: Plastic surgery.
		 Float: Floating point type.
		 Boolean: Pass True/False in.
		 DECIMAL: Fixed-point type.
		 enum: enumerated type.
		 Date: Pass datetime.date() in. 2020 10 28
		def add_data():
		 user = User (add attribute data)
		session.add(user)
		sesssion.commit()
		def search_data():
		 data = session.query(Usre).filter(conditional judgment).all()
		print(data)
		def update_data():
		 data = session.query(Usre).filter(conditional judgment).all()
		 data.attribute name = xxx
		session.commit()
		def delete_data():
		 data = session.query(Usre).filter(conditional judgment).all()
		session.rollback()
		session.delete(data)
		 DateTime: Pass datetime.datetime() in. 2020 10 28 21 36 21
		 Time: Pass datetime.time() in. 21 36 21
		 String: Character type. You need to specify the length when using it, which is different from the Text type.
		 Text: Text type.
		 LONGTEXT: Long text type.

Column commonly used parameters and aggregate functions

Common parameters
default: The default value. When you set default=xxx, if you do not pass this data, then the database will display your settings
Of this value
nullable: Whether it is nullable. nullable=False means it cannot be empty
primary_key: Whether it is the primary key.
unique: Is it unique? When you set this parameter, then the data in the unified column cannot be the same, and the same will report an error
autoincrement: Whether to increase automatically.
onupdate: The function to be executed when updating.
name: the field mapping of the attribute in the database

Aggregate function
func.count
func.max
func.min
func.avg
func.sum

Intelligent Recommendation

SQLAlchemy attributes commonly used data types-filter conditions

Filter condition Filtering is a very important function of data extraction. Here is an explanation of some commonly used filter conditions, and these filter conditions can only be achieved through the...

Chapter 46 ORM model, commonly used fields and addition, deletion, modification and query operations

Experience: I receive a lot of information and knowledge every day. I'm afraid I can't remember what I should remember. I have to summarize it systematically to deepen my memory. I hope I will become ...

flask+SQLAlchemy+mysql (addition, deletion and modification)

Directory Structure Need to install the library: Step 1: Configure database related information settings.py Step 2: Build a database model models.py Step 3: __init__.py file in the blog directory, not...

MongoDB commonly used addition, deletion and modification

1. Basically increased, deleted and modified 2. Group query according to conditions...

More Recommendation

Commonly used aggregate functions

When it comes to aggregate functions, they must be very unfamiliar. Today I will introduce commonly used aggregate functions. Aggregation functions can be described as old roles that are often used in...

Topic: Introduction to MySQL (1) Database/Commonly Used Engines/Addition, Deletion, and Modification of Tables and Data

1. Introduction to the database The database is used to store data, the data is not directly stored in the database, the database is stored in the table, and the data is stored in the table. So our le...

Python full stack (7) Flask framework 7.ORM addition, deletion, modification, query, data type and parameters

ORM addition, deletion, modification, data type and parameters 1. Flask-ORM add data 2. Adding, deleting, modifying and checking Flask-ORM data Three, SQLAlchemy commonly used data types Four, Column ...

[SQLAlchemy] Section 3: Commonly used fields and their constraints

[SQLAlchemy] Section 3: Commonly used fields and their constraints 1 Guide package 2 Commonly used field types 3 Common constraints 4 Examples 1 Guide package 2 Commonly used field types Keyword Corre...

ORM Column of the common parameters sqlalchemy

default: default value nullable: is empty primary_key: primary key unique: is unique autoincrement: Are increment onupdate: execution of updating name: the property mapping database  ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top