SqlAlchemy ORM
2016-03-31 23:09
495 查看
SqlAlchemy ORM
SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据API执行SQL并获取执行结果Dialect用于和数据API进行交流,根据配置文件的不同调用不同的数据库API,从而实现对数据库的操作,如:
步骤一:
使用 Engine/ConnectionPooling/Dialect 进行数据库操作,Engine使用ConnectionPooling连接数据库,然后再通过Dialect执行SQL语句。
步骤二:
使用 Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 进行数据库操作。Engine使用Schema Type创建一个特定的结构对象,之后通过SQL Expression Language将该对象转换成SQL语句,然后通过 ConnectionPooling 连接数据库,再然后通过 Dialect 执行SQL,并获取结果。
+ View Code
增删改查
+ View Code
一个简单的完整例子
更多内容详见:
http://www.jianshu.com/p/e6bba189fcbd http://docs.sqlalchemy.org/en/latest/core/expression_api.html
注:SQLAlchemy无法修改表结构,如果需要可以使用SQLAlchemy开发者开源的另外一个软件Alembic来完成。
步骤三:
使用 ORM/Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 所有组件对数据进行操作。根据类创建对象,对象转换成SQL,执行SQL。
外键关联
A one to many relationship places a foreign key on the child table referencing the parent.relationship()is then specified on the parent, as referencing a collection of items represented by the child
from sqlalchemy import Table, Column, Integer, ForeignKey from sqlalchemy.orm import relationship from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()
relationship()and connect the two using the
relationship.back_populatesparameter:
Childwill get a
parentattribute with many-to-one semantics.
Alternatively, the
backrefoption may be used on a single
relationship()instead of using
back_populates:
附,原生sql join查询
几个Join的区别 http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-joins
INNER JOIN: Returns all rows when there is at least one match in BOTH tables
LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
group by 查询
相关文章推荐
- Oracle SQL Developer - 智能提示
- MEMCACHED学习二、基本操作命令stats
- PL/SQL调用BIEE WebServices清理BI Server缓存
- 搭建 Sql Server 2008 R2 Failover Cluster
- mybatis——动态sql
- 跟我一起学习MySQL技术内幕(第五版):(第一章学习日记5)
- 从环信拉取消息记录,写入本地数据库
- mysql保留元数据的情况下修改表结构
- 不停止MySQL服务增加从库的两种方式
- 在Node.js使用mysql模块时遇到的坑
- distinct使SQL查询除去重复的字段
- MySQL的转义字符“\”
- mysql常用字符串操作函数大全,以及实例
- mysql update语句的用法详解
- Redis内存使用优化与存储
- oracle10g安装,卸载
- mongoDB——split&balance操作
- 写出一条Sql语句,取出表A中的第31条到第40条记录。表A以自动增长的ID作为主键。(注意:ID可能不是连续的)
- Redis 服务器的安装与启动
- oracle11g 在112G内存中如何使用hugepage提高性能