【脚本语言系列】关于Python数据库处理SQLAlchemy,你需要知道的事
来源:互联网 发布:sublime json 格式化 编辑:程序博客网 时间:2024/06/05 04:30
如何使用SQLAlchemy
使用引擎层
# -*- coding:utf-8 -*-import sqlalchemy as saconn = sa.create_engine('sqlite://')conn.execute('''CREATE TABLE zoo(critter VARCHAR(20) PRIMARY KEY,count INT,damages FLOAT)''')ins = "INSERT INTO zoo (critter, count, damages) VALUES(?,?,?)"print conn.execute(ins, ("duck",5, 0.0))print conn.execute(ins, ("bear",2, 1000.0))print conn.execute(ins, ("weasel", 1, 2000.0))rows = conn.execute("SELECT * FROM zoo")for row in rows: print row
<sqlalchemy.engine.result.ResultProxy object at 0x0000000006C9FAC8><sqlalchemy.engine.result.ResultProxy object at 0x0000000006CFA0F0><sqlalchemy.engine.result.ResultProxy object at 0x0000000006CFA198>(u'duck', 5, 0.0)(u'bear', 2, 1000.0)(u'weasel', 1, 2000.0)
使用中间层
# -*- coding:utf-8 -*-import sqlalchemy as saconn = sa.create_engine('sqlite://')meta = sa.MetaData()zoo = sa.Table("zoo",meta, sa.Column("critter",sa.String,primary_key=True), sa.Column("count",sa.Integer), sa.Column("damages",sa.Float) )meta.create_all(conn)print conn.execute(zoo.insert(("duck",5, 0.0)))print conn.execute(zoo.insert(("bear",2, 1000.0)))print conn.execute(zoo.insert(("weasel", 1, 2000.0)))result = conn.execute(zoo.select())rows = result.fetchall()print(rows)
<sqlalchemy.engine.result.ResultProxy object at 0x0000000006CFA400><sqlalchemy.engine.result.ResultProxy object at 0x0000000006CFA400><sqlalchemy.engine.result.ResultProxy object at 0x0000000006CFA668>[(u'duck', 5, 0.0), (u'bear', 2, 1000.0), (u'weasel', 1, 2000.0)]
使用顶层
# -*- coding:utf-8 -*-import sqlalchemy as safrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy.orm import sessionmakerconn = sa.create_engine('sqlite:///zoo.db')Base = declarative_base()class Zoo(Base): __tablename__="zoo" critter = sa.Column("critter", sa.String, primary_key = True) count = sa.Column("count", sa.Integer) damages = sa.Column("damages", sa.Float) def __init__(self, critter, count, damages): self.critter = critter self.count = count self.damages = damages def __repr__(self): return "<Zoo({},{},{}>".format(self.critter, self.count, self.damages)Base.metadata.create_all(conn)first = Zoo("duck",10,0.0)second = Zoo("bear",2,1000.0)third = Zoo("weasel",1,2000.0)Session = sessionmaker(bind=conn)session = Session()session.add(first)session.add_all([second, third])session.commit()
什么是SQLAlchemy
Python DB-API实现所有的关系型数据库的SQL语言的交集。
SQLAlchemy用来实现每种关系型数据库的SQL语言的合集。
其为分层结构:
底层引擎层,类似DB-API,处理数据库连接,执行SQL命令和返回结果;
中间层类似SQL生成器,处理SQL方言之间的差异;
顶层是对象关系模型,使用SQL表达式语言,隐藏实际数据库的机制。
阅读全文
1 0
- 【脚本语言系列】关于Python数据库处理SQLAlchemy,你需要知道的事
- 【脚本语言系列】关于Python数据库处理SQLite数据库,你需要知道的事
- 【脚本语言系列】关于Python数据库处理SQLite,你需要知道的事
- 【脚本语言系列】关于Python数据库处理MySQL, 你需要知道的事
- 【脚本语言系列】关于Python系统管理,你需要知道的事
- 【脚本语言系列】关于Python容器,你需要知道的事
- 【脚本语言系列】关于PythonNoSQL数据库处理dbm,你需要知道的事
- 【脚本语言系列】关于PythonNoSQL数据库处理memcached,你需要知道的事
- 【脚本语言系列】关于PythonNoSQL数据库处理redis,你需要知道的事
- 【脚本语言系列】关于Python音频处理DirectSound, 你需要知道的事
- 【脚本语言系列】关于Python图像处理PIL,你需要知道的事
- 【脚本语言系列】关于Python基础知识处理字符串,你需要知道的事
- 【脚本语言系列】关于Python异常处理,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理YAML,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理csv,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理xml,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理HTML,你需要知道的事
- 【脚本语言系列】关于Python结构化文本文件处理JSON,你需要知道的事
- 算法作业_37(2017.6.19第十八周)
- Android UI测试框架Espresso以及代码覆盖率jacoco的使用
- npm常用命令
- 接续符和转义符
- DPCM编解码
- 【脚本语言系列】关于Python数据库处理SQLAlchemy,你需要知道的事
- NodeJs新手入门(三)
- JAVA基础IO-fileReader字符流
- 关于STL中的堆栈Stack
- SQL 【INNER JOIN 关键字】
- webstorm 2016, 2017 激活破解
- jQuery+AJAX实现无刷新分页滚动下拉加载
- Android 不错的控件和框架
- ThreadLocal实现原理以及内存泄漏问题