sqlalchemy

How to catch specific exceptions on sqlalchemy?

有些话、适合烂在心里 提交于 2020-12-12 11:49:06
问题 I want to catch specific exceptions like UniqueViolation on sqlalchemy. But sqlalchemy throw exceptions only through IntegrityError . So I catched specific exceptions with below code. except sqlalchemy.exc.IntegrityError as e: from psycopg2 import errors if isinstance(e.orig, errors.UniqueViolation): pass elif isinstance(e.orig, errors.ForeignKeyViolation): pass But it looks doesn't elegant. I don't want to using if statement just catch with specific exception name. Is there any way to solve

Sqlalchemy数据类型定义

做~自己de王妃 提交于 2020-12-11 23:44:44
与所有 SQLAlchemy 方言一样,已知对 MySQL 有效的所有大写类型都可以从顶级方言导入 from sqlalchemy.dialects.mysql import \ BIGINT, BINARY, BIT, BLOB, BOOLEAN, CHAR, DATE, \ DATETIME, DECIMAL, DECIMAL, DOUBLE, ENUM, FLOAT, INTEGER, \ LONGBLOB, LONGTEXT, MEDIUMBLOB, MEDIUMINT, MEDIUMTEXT, NCHAR, \ NUMERIC, NVARCHAR, REAL, SET, SMALLINT, TEXT, TIME, TIMESTAMP, \ TINYBLOB, TINYINT, TINYTEXT, VARBINARY, VARCHAR, YEAR 来源: oschina 链接: https://my.oschina.net/u/2440810/blog/4792492

SQLAlchemy: Can't reconnect until invalid transaction is rolled back

好久不见. 提交于 2020-12-09 07:10:10
问题 I have a weird problem. I have a simple py3 app, which uses sqlalchemy. But several hours later, there is an error: (sqlalchemy.exc.InvalidRequestError) Can't reconnect until invalid transaction is rolled back My init part: self.db_engine = create_engine(self.db_config, pool_pre_ping=True) # echo=True if needed to see background SQL Session = sessionmaker(bind=self.db_engine) self.db_session = Session() The query (this is the only query that happens): while True: device_id = self.db_session

SQLAlchemy: Can't reconnect until invalid transaction is rolled back

こ雲淡風輕ζ 提交于 2020-12-09 07:09:08
问题 I have a weird problem. I have a simple py3 app, which uses sqlalchemy. But several hours later, there is an error: (sqlalchemy.exc.InvalidRequestError) Can't reconnect until invalid transaction is rolled back My init part: self.db_engine = create_engine(self.db_config, pool_pre_ping=True) # echo=True if needed to see background SQL Session = sessionmaker(bind=self.db_engine) self.db_session = Session() The query (this is the only query that happens): while True: device_id = self.db_session

SQLAlchemy: Can't reconnect until invalid transaction is rolled back

我的未来我决定 提交于 2020-12-09 07:08:50
问题 I have a weird problem. I have a simple py3 app, which uses sqlalchemy. But several hours later, there is an error: (sqlalchemy.exc.InvalidRequestError) Can't reconnect until invalid transaction is rolled back My init part: self.db_engine = create_engine(self.db_config, pool_pre_ping=True) # echo=True if needed to see background SQL Session = sessionmaker(bind=self.db_engine) self.db_session = Session() The query (this is the only query that happens): while True: device_id = self.db_session

SQL Alchemy Won't Drop Tables at end of Test due to MetaData Lock on db

对着背影说爱祢 提交于 2020-12-06 13:51:16
问题 I am working on testing my flask app model. I'm using mysql 5.7, sqlalchemy and pytest. Within my model, I have a CRUD mixin that I used to manage creating, updating and deleting. Whenever I try to access the object in the Mixin before returning the object to the test function, SQLAlchemy hangs at db.drop_all in my tear down. When I look in mysql at PROCESSLIST, it shows 1 sleep query and 1 query waiting for table metadata lock. I can fix this by calling db.session.commit in the create method

SQL Alchemy Won't Drop Tables at end of Test due to MetaData Lock on db

ε祈祈猫儿з 提交于 2020-12-06 13:50:32
问题 I am working on testing my flask app model. I'm using mysql 5.7, sqlalchemy and pytest. Within my model, I have a CRUD mixin that I used to manage creating, updating and deleting. Whenever I try to access the object in the Mixin before returning the object to the test function, SQLAlchemy hangs at db.drop_all in my tear down. When I look in mysql at PROCESSLIST, it shows 1 sleep query and 1 query waiting for table metadata lock. I can fix this by calling db.session.commit in the create method

python pandas: export structure only (no rows) of a dataframe to SQL

一曲冷凌霜 提交于 2020-12-06 04:33:32
问题 I am using pandas 0.16 and sqlalchemy. Is it possible to export just the structure, i.e. column names and data types but no rows, of a dataframe to SQL? The closest I managed to get to was to export the first row only: df.ix[[0],:].to_sql( tablename, myconnection ) And then I'd have to do a truncate table. However, there are inconsistencies between the to_csv and the to_sql methods: to_csv writes boolean fields as the strings 'TRUE' or 'FALSE' , whereas to_sql writes them as 0 or 1. This

SQLAlchemy和Flask-SQLAlchemy的使用

你离开我真会死。 提交于 2020-12-06 04:09:26
1. SQLAlchemy ORM框架、通用 Django-Model:基于django 1. 安装 pip install sqlalchemy 2. 使用 1. 约束 primary_key auto_increment nullable index unique 2. 数据类型 INT、INTEGER、Integer:都是整型 CHAR、 NCHAR、VARCHAR、 NVARCHAR、String:都是字符串 # 声明一个基类 from sqlalchemy.ext.declarative import declarative_base BaseModel = declarative_base() from sqlalchemy import Column, INT, String # ORM:object relationship mapping class User(BaseModel): # 创建一个table __tablename__ = 'user' id = Column(INT, primary_key=True, auto_increment=True) name = Column(String(32), nullable=False, index=True, unique=True) # 数据库引擎创建 from sqlalchemy.engine

SQLAlchemy和Flask-SQLAlchemy

天大地大妈咪最大 提交于 2020-12-05 19:53:22
一、ORM 与 SQLAlchemy 简介 ORM 全称 Object Relational Mapping , 翻译过来叫 对象关系映射 。简单的说,ORM 将数据库中的表与面向对象语言中的类建立了一种对应关系。这样,我们要操作数据库,数据库中的表或者表中的一条记录就可以直接通过操作类或者类实例来完成。 SQLAlchemy 是Python 社区最知名的 ORM 工具之一,为高效和高性能的数据库访问设计,实现了完整的企业级持久模型。 二、SQLAlchemy的使用 1、创建单表结构: from sqlalchemy.ext.declarative import declarative_base # 导入基类 from sqlalchemy import Column, Integer, String # 数据类型 # Base = ORM基类 - 要按照ORM的规则定义你的类 Base = declarative_base() class Users(Base): __tablename__ = " user " # 创建ID数据字段 , 那么ID是不是一个数据列呢? 也就是说创建ID字段 == 创建ID数据列 # id = Column(数据类型,索引,主键,外键,等等) id = Column(Integer, primary_key=True, autoincrement