栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

SQLAlchemy

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SQLAlchemy

What is the SQLAlchemy

  • Python SQL Toolkit (most popularly used)
    • End-to-end tools for working with relational databases without writing SQL
  • Offers an ORM (Object Relational Mapping library), which provides an interface for using object oriented programming to interact with a database
    • Maps tables and columns to class objects and attributes.

Writing SQLAlchemy vs raw SQL
In SQL

CREATE TABLE todos (
   id INTEGER PRIMARY KEY,
   description VARCHAR NOT NULL,
   completed BOOLEAN NOT NULL DEFAULT false
);
SELECt * FROM todos;

In SQLAlchemy ORM

class Todo(db.Model):
	id = db.Column(db.Integer, primary_key = True)
	description = db.Column(db.String(), nullable=False)
	completed = db.Column(db.Boolean, nullable=False, default=False)
Todo.query.all()

Why use SQLAlchemy over writing raw SQL?

  • Work entirely in Python. Don’t write raw SQL anymore.
    • Features function-based query constructions: allows SQL clauses to be built via Python functions and expressions.
  • Avoid making errors in SQL syntax. It generates SQL and Python code for you to access tables, which leads to less database-related overhead in terms of the volume of code you need to write overall to interact with your models.
    • more rapid web development
  • We can forget about the database system we’re using
    • Switch between SQLite for development, and Postgres for production
  • Moreover, you can avoid sending SQL to the database on every call. The SQLAlchemy ORM library features automatic caching, caching collections and references between objects once initially loaded.

Other ORM libraries that exist across other languages include popular choices like javascript libraries Sequelize and Bookshelf.js for NodeJS applications, the ruby library ActiveRecord, which is used inside Ruby on Rails, and CakePHP for applications written on PHP, amongst many other such ORMs.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/444485.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号