Steps to use ORM to add data in MySQL

Steps to use ORM to add data in MySQL

【Foreword】

If you want to use ORM to operate data in the database, the premise is that you have created a new ORM model; that is, the model we have built before https://www.jb51.net/article/218036.htm

[ORM operation steps]

  • Constructing ORM model objects
  • Add to session
  • Submit to database
  • If an exception occurs, roll back the transaction (optional)

【Constructing ORM model objects】

user_obj = User(
username="use1",
password="123456",
real_name = "User 1",
age=12
)

【Add to session】

# Add an object session.add(user_obj)

# Add multiple objects session.add_all([user1,user2,user3])

[The role of session]

  • Establishing a session with the database
  • Use session to add, delete, modify and query data in the database
  • Use session for transaction control (commit and rollback)
  • You need to close it after use session.close()

[Creating a session]

There are two methods, the following code example

# Method 1: Instantiate session
from sqlalchemy.orm import Session

with Session(engine) as session:
    session.add(User())
    session.commit()

# Method 2: Create a factory function from sqlalchemy.orm import sessionmaker

Session = sessionmaker(engine)

with Session.begin() as session:
    session.add(User())

[Submit data to database]

with Session(engine) as session:
    session.add(user_obj)
    session.add_all([user1,user1,user1])
    session.commit()

[Exception occurred, rollback transaction]

with Session(engine) as session:
    session.begin()
    try:
        session.add(user1)
        session.add(user2)
    except:
        session.rollback()
        raise
    else:
        session.commit()

[Other common methods in Session objects]

1. execute(statement, params=None,*args) executes SQL query

2. delete(instance) physically deletes data

3. get(entity,idnet,*args) returns the ORM object that meets the conditions according to the primary key/None

4. query(*entities,**kwargs)

Use ORM query to return Query object

This is the end of this article about using ORM to add data in Mysql. For more relevant ORM MySQL database content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to operate MySQL database with ORM model framework
  • How to operate MySql database with gorm
  • How to operate MySQL database using orm in .net core
  • PHP operates MySQL database instance based on ORM
  • Can information_schema and mysql in mysql database be deleted?
  • Parsing MySQL's information_schema database

<<:  How to use Docker+DockerCompose to encapsulate web applications

>>:  Share 20 excellent web form design cases

Recommend

How to remove the dividing line of a web page table

<br />How to remove the dividing lines of a ...

Implementation of debugging code through nginx reverse proxy

background Now the company's projects are dev...

How to make ApacheBench support multi-url

Since the standard ab only supports stress testin...

Analysis of JavaScript's event loop mechanism

Table of contents Preface: 1. Reasons for the eve...

Detailed tutorial on deploying Springboot or Nginx using Kubernetes

1 Introduction After "Maven deploys Springbo...

Example code for implementing a simple search engine with MySQL

Table of contents Preface Introduction ngram full...

A general method for implementing infinite text carousel with native CSS

Text carousels are very common in our daily life....

Linux Cron scheduled execution of PHP code with parameters

1. Still use PHP script to execute. Command line ...

Explain TypeScript enumeration types in detail

Table of contents 1. Digital Enumeration 2. Strin...

Implementation of the login page of Vue actual combat record

Table of contents 1. Preliminary preparation 1.1 ...

Don't forget to close the HTML tag

Building web pages that comply with Web standards ...

SQL query for users who have placed orders for at least seven consecutive days

Create a table create table order(id varchar(10),...