Automap sqlalchemy.ext.automap 自动映射数据库表结构

本文介绍了使用SQLAlchemy的自动映射功能从现有数据库表结构中动态生成ORM类的方法。通过示例展示了如何连接数据库、反射表结构以及操作数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine

Base = automap_base()

# engine, suppose it has two tables 'user' and 'address' set up
engine = create_engine("mysql+mysqldb://wise:wise@172.31.238.141/wise?charset=utf8")

# reflect the tables
Base.prepare(engine, reflect=True)

# mapped classes are now created with names by default
# matching that of the table name.
User = Base.classes.user
Address = Base.classes.address


session = Session(engine)

# rudimentary relationships are produced
#session.add(Address(email_address="foo@bar.com", user=User(name="foo")))
#session.commit()

# collection-based relationships are by default named
# "<classname>_collection"

http://www.buluo360.com/

转载于:https://www.cnblogs.com/tensorflownews/p/7451878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值