SQLAlchemy Core中的异常及事务处理样码
这部门内容比较简单,立存。
#coding=utf-8 from datetime import datetime from sqlalchemy import (MetaData, Table, Column, Integer, Numeric, String, Boolean, DateTime, ForeignKey, ForeignKey, create_engine, CheckConstraint) from sqlalchemy import (insert, select, update, delete, text, desc, cast, and_, or_, not_) from sqlalchemy.sql import func from sqlalchemy.exc import IntegrityError metadata = MetaData() cookies = Table('cookies', metadata, Column('cookie_id', Integer(), primary_key=True), Column('cookie_name', String(50), index=True), Column('cookie_recipe_url', String(255)), Column('cookie_sku', String(55)), Column('quantity', Integer()), Column('unit_cost', Numeric(12, 2)), CheckConstraint('quantity >= 0', name='quantity_positive') ) users = Table('users', metadata, Column('user_id', Integer(), primary_key=True), Column('username', String(15), nullable=False, unique=True), Column('email_address', String(255), nullable=False), Column('phone', String(20), nullable=False), Column('password' ,String(25), nullable=False), Column('created_on', DateTime(), default=datetime.now), Column('updated_on', DateTime(), default=datetime.now, onupdate=datetime.now) ) orders = Table('orders', metadata, Column('order_id', Integer(), primary_key=True), Column('user_id', ForeignKey('users.user_id')), Column('shipped', Boolean(), default=False) ) line_items = Table('line_items', metadata, Column('line_items_id', Integer(), primary_key=True), Column('order_id', ForeignKey('orders.order_id')), Column('cookie_id', ForeignKey('cookies.cookie_id')), Column('quantity', Integer()), Column('extended_cost', Numeric(12, 2)) ) engine = create_engine('mysql+pymysql://u:p@ip:3306/cookies') metadata.create_all(engine) connection = engine.connect() def ship_it(order_id): s = select([line_items.c.cookie_id, line_items.c.quantity]) s = s.where(line_items.c.order_id == order_id) transaction = connection.begin() cookies_to_ship = connection.execute(s) try: for cookie in cookies_to_ship: u = update(cookies).where(cookies.c.cookie_id==cookie.cookie_id) u = u.values(quantity = cookies.c.quantity - cookie.quantity) result = connection.execute(u) u = update(orders).where(orders.c.order_id == order_id) u = u.values(shipped=True) result = connection.execute(u) print("Shipped order ID: {}".format(order_id)) transaction.commit() except IntegrityError as error: transaction.rollback() print(error) ''' ins = insert(users).values( username="cookiemon", email_address="mon@cookie.com", phone="111-111-1111", password="password" ) try: result = connection.execute(ins) except IntegrityError as error: pass ins = cookies.insert() inventory_list = [ { 'cookie_name': 'chocolate chip', 'cookie_recipe_url': 'http://some.aweso.me/cookie/recipe.html', 'cookie_sku': 'CC01', 'quantity': '12', 'unit_cost': '0.50' }, { 'cookie_name': 'dark chocolate chip', 'cookie_recipe_url': 'http://some.aweso.me/cookie/recipe_dark.html', 'cookie_sku': 'CC02', 'quantity': '1', 'unit_cost': '0.75' } ] result = connection.execute(ins, inventory_list) ins = insert(orders).values(user_id=1, order_id='1') result = connection.execute(ins) ins = insert(line_items) order_items = [ { 'order_id': 1, 'cookie_id': 1, 'quantity': 9, 'extended_cost': 4.50 } ] result = connection.execute(ins, order_items) ins = insert(orders).values(user_id=1, order_id='2') result = connection.execute(ins) ins = insert(line_items) order_items = [ { 'order_id': 2, 'cookie_id': 1, 'quantity': 4, 'extended_cost': 1.50 }, { 'order_id': 2, 'cookie_id': 2, 'quantity': 1, 'extended_cost': 4.50 } ] result = connection.execute(ins, order_items) ship_it(1) s = select([cookies.c.cookie_name, cookies.c.quantity]) connection.execute(s).fetchall() ''' ship_it(20)
SQLAlchemy Core中的异常及事务处理样码的更多相关文章
- ASP.Net Core中处理异常的几种方法
本文将介绍在ASP.Net Core中处理异常的几种方法 1使用开发人员异常页面(The developer exception page) 2配置HTTP错误代码页 Configuring stat ...
- .net core 的图片处理及二维码的生成及解析
写代码这事,掐指算来已经十有余年. 从html到css到javascript到vbscript到c#,从兴趣到职业,生活总是失落与惊喜并存. 绝大部分时候,出发并不是因为知道该到哪里去,只是知道不能再 ...
- TransactionScope事务处理方法介绍及.NET Core中的注意事项
作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/10170712.html 今天在写CzarCms的UnitOfWork的使用使用到了这个Transacti ...
- TransactionScope事务处理方法介绍及.NET Core中的注意事项 SQL Server数据库漏洞评估了解一下 预热ASP.NET MVC 的VIEW [AUTOMAPPER]反射自动注册AUTOMAPPER PROFILE
TransactionScope事务处理方法介绍及.NET Core中的注意事项 作者:依乐祝 原文链接:https://www.cnblogs.com/yilezhu/p/10170712.ht ...
- 【转】TransactionScope事务处理方法介绍及.NET Core中的注意事项
什么是TransactionScope呢? TransactionScope作为System.Transactions的一部分被引入到.NET 2.0.同时SqlClient for .NET Cor ...
- ASP.NET Core中,UseDeveloperExceptionPage扩展方法会吃掉异常
在ASP.NET Core中Startup类的Configure方法中,有一个扩展方法叫UseDeveloperExceptionPage,如下所示: // This method gets call ...
- 如何处理Entity Framework / Entity Framework Core中的DbUpdateConcurrencyException异常(转载)
1. Concurrency的作用 场景有个修改用户的页面功能,我们有一条数据User, ID是1的这个User的年龄是20, 性别是female(数据库中的原始数据)正确的该User的年龄是25, ...
- Linux中生成Core Dump系统异常信息记录文件的教程
Linux中生成Core Dump系统异常信息记录文件的教程 http://www.jb51.net/LINUXjishu/473351.html
- 项目开发中的一些注意事项以及技巧总结 基于Repository模式设计项目架构—你可以参考的项目架构设计 Asp.Net Core中使用RSA加密 EF Core中的多对多映射如何实现? asp.net core下的如何给网站做安全设置 获取服务端https证书 Js异常捕获
项目开发中的一些注意事项以及技巧总结 1.jquery采用ajax向后端请求时,MVC框架并不能返回View的数据,也就是一般我们使用View().PartialView()等,只能返回json以 ...
随机推荐
- hdu1455 dfs+剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 2016 Multi-university training contest
day 1 A 给G,w(e)1M(diff),|V|100K,|E|1M,求 MST MST上任意两点间距离的期望 显然MST唯一 E(dis(u,v))可以通过计算每条边的贡献加出来 B n个并行 ...
- MappingJacksonHttpMessageConverter过期
最新在做一个SSM整合学习项目,maven加上jackson库,spring配置json,tomcat不能正常部署启动 Maven配置 <!-- 引入jackson,防止ajax调用返回json ...
- HTK学习2:工具使用
选自:http://www.cnblogs.com/mingzhao810/archive/2012/08/03/2617674.html 这个是重点,呵呵,本部分会讨论到如下内容: 1. 建立语音材 ...
- Spring Boot+AngularJS中因为跨域导致Session丢失
http://blog.csdn.net/dalangzhonghangxing/article/details/52446821 如果还在为跨域问题烦恼,请查看博主的 解决angular+sprin ...
- Java web 开发环境搭建
1.下载jdk-8u60-windows-i586.exe;安装过程会让指定jre的安装路径,此处最好指定和jdk安装一个目录下(D:\Program Files\Java\jdk1.8.0_60,D ...
- Linux下查看某一进程所占用内存的方法
Linux下查看某一个进程所占用的内存,首先可以通过ps命令找到进程id,比如 ps -ef | grep kafka 可以看到kafka这个程序的进程id 可以看到是2913,现在可以使用如下命令查 ...
- percona-toolkit 之 【pt-slave-delay】说明
摘要: 在主从复制的架构中,正常情况下主上操作的记录也会在从上进行操作,虽说是异步复制,但操作会“实时”的同步到从.比如在主上不小心误操作了,还没等反应过来从上也会马上执行误操作,后期只有通过二进制或 ...
- ffmpeg-20160520-git-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- HTML DOM scale() 方法
语法 scale(sx, sy) 参数 参数 描述 sx, sy 水平和垂直的缩放因子. 描述 scale() 方法为画布的当前变换矩阵添加一个缩放变换.缩放通过独立的水平和垂直缩放因子来完成.例如, ...