sqlalchemy

How to solve error “Operator 'getitem' is not supported on this expression” when using case()

社会主义新天地 提交于 2020-12-21 03:52:30
问题 I'm trying to convert the following SQL into SQLAlchemy: select t1.id, t1.field_A, max(case when t2.field_B = 1 then t2.field_C end) test_2_field_b_1, max(case when t2.field_B = 2 then t2.field_C end) test_2_field_b_2 from test_1 t1 inner join test_2 t2 on t2.field_A = t1.field_A group by t1.id, t1.field_A I've got as far as: qry = session.query( Test1.id_, Test2.field_A, func.max(case((Test2.field_B.__eq__(1), "Test2.field_C"))).label("test_2_field_b_1"), func.max(case((Test2.field_B.__eq__

How to solve error “Operator 'getitem' is not supported on this expression” when using case()

早过忘川 提交于 2020-12-21 03:51:38
问题 I'm trying to convert the following SQL into SQLAlchemy: select t1.id, t1.field_A, max(case when t2.field_B = 1 then t2.field_C end) test_2_field_b_1, max(case when t2.field_B = 2 then t2.field_C end) test_2_field_b_2 from test_1 t1 inner join test_2 t2 on t2.field_A = t1.field_A group by t1.id, t1.field_A I've got as far as: qry = session.query( Test1.id_, Test2.field_A, func.max(case((Test2.field_B.__eq__(1), "Test2.field_C"))).label("test_2_field_b_1"), func.max(case((Test2.field_B.__eq__

How to solve error “Operator 'getitem' is not supported on this expression” when using case()

吃可爱长大的小学妹 提交于 2020-12-21 03:49:22
问题 I'm trying to convert the following SQL into SQLAlchemy: select t1.id, t1.field_A, max(case when t2.field_B = 1 then t2.field_C end) test_2_field_b_1, max(case when t2.field_B = 2 then t2.field_C end) test_2_field_b_2 from test_1 t1 inner join test_2 t2 on t2.field_A = t1.field_A group by t1.id, t1.field_A I've got as far as: qry = session.query( Test1.id_, Test2.field_A, func.max(case((Test2.field_B.__eq__(1), "Test2.field_C"))).label("test_2_field_b_1"), func.max(case((Test2.field_B.__eq__

Getting COUNT from sqlalchemy

喜你入骨 提交于 2020-12-20 08:10:50
问题 I have: res = db.engine.execute('select count(id) from sometable') The returned object is sqlalchemy.engine.result.ResultProxy . How do I get count value from res ? Res is not accessed by index but I have figured this out as: count=None for i in res: count = res[0] break There must be an easier way right? What is it? I didn't discover it yet. Note: The db is a postgres db. 回答1: While the other answers work, SQLAlchemy provides a shortcut for scalar queries as ResultProxy.scalar(): count = db

Getting COUNT from sqlalchemy

拟墨画扇 提交于 2020-12-20 08:08:11
问题 I have: res = db.engine.execute('select count(id) from sometable') The returned object is sqlalchemy.engine.result.ResultProxy . How do I get count value from res ? Res is not accessed by index but I have figured this out as: count=None for i in res: count = res[0] break There must be an easier way right? What is it? I didn't discover it yet. Note: The db is a postgres db. 回答1: While the other answers work, SQLAlchemy provides a shortcut for scalar queries as ResultProxy.scalar(): count = db

python----python使用mysql

|▌冷眼眸甩不掉的悲伤 提交于 2020-12-18 19:23:05
Python操作MySQL主要使用两种方式:   原生模块 pymsql   ORM框架 SQLAchemy pymql pymsql是Python中操作MySQL的模块,在windows中的安装: pip install pymysql 入门:我们连接虚拟机中的centos中的mysql,然后查询test数据库中student表的数据 import pymysql # 创建连接 conn = pymysql.connect(host= ' 192.168.123.207 ' ,port=3306,user= ' root ' ,passwd= ' root ' ,db= " test " ); # 创建游标 cursor = conn.cursor() # 执行sql,并返回受影响的行数 effect_row = cursor.execute( " select * from student " ) print (effect_row) 运行结果: 需要给权限 mysql> grant all on *.* to 'root'@'%' identified by 'root' ; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql > flush privileges; Query OK, 0 rows affected (

python怎么和MySQL链接?

守給你的承諾、 提交于 2020-12-18 12:00:26
  python怎么和mysql连接?目前来说,有3种基本方法,分别是pymysql、sqlalchemy和pandas,下面我分别简单介绍一下,感兴趣的朋友可以尝试一下:   01、pymysql   这是最基础、也是最基本的python操作mysql的第三方包,相信许多朋友都接触过,封装了对mysql的基本操作,包括增删改查等,只需几行代码即可轻松完成对mysql读写,简单易用、非常容易学习,安装的话,直接在cmd窗口输入命令“pip install pymysql”即可:   安装完成后,我们就可以直接对mysql进行操作了,如下,先创建连接,然后获取游标,最后直接执行sql语句即可,这里需要注意的是,增删改后不要忘记commit提交更新数据库,否则数据不会更新:   02、sqlalchemy   这是基于对象关系映射模型ORM开发的一个开源数据库框架,将python类和数据表关联起来,摒弃了底层繁琐的数据库操作,只需编写一套代码即可在主流的数据库之间来回切换,操作简单、便于移植,安装的话,直接在cmd窗口输入命令“pip install sqlalchemy”即可,程序会自动检测相关依赖并安装:   安装完成后,我们就可以直接开始数据库操作了,如下,先创建数据库连接,然后定义对象类(对应于具体数据表),最后直接进行增删改查即可,需要注意的是

Join on a CTE in SQLAlchemy

╄→尐↘猪︶ㄣ 提交于 2020-12-15 06:43:30
问题 I'm trying to formulate a SQLAlchemy query that uses a CTE to build a table-like structure of an input list of tuples, and JOIN it with one of my tables (backend DB is Postgres). Conceptually, it would look like: WITH to_compare AS ( SELECT * FROM ( VALUES (1, 'flimflam'), (2, 'fimblefamble'), (3, 'pigglywiggly'), (4, 'beepboop') -- repeat for a couple dozen or hundred rows ) AS t (field1, field2) ) SELECT b.field1, b.field2, b.field3 FROM my_model b JOIN to_compare c ON (c.field1 = b.field1)

Join on a CTE in SQLAlchemy

懵懂的女人 提交于 2020-12-15 06:41:46
问题 I'm trying to formulate a SQLAlchemy query that uses a CTE to build a table-like structure of an input list of tuples, and JOIN it with one of my tables (backend DB is Postgres). Conceptually, it would look like: WITH to_compare AS ( SELECT * FROM ( VALUES (1, 'flimflam'), (2, 'fimblefamble'), (3, 'pigglywiggly'), (4, 'beepboop') -- repeat for a couple dozen or hundred rows ) AS t (field1, field2) ) SELECT b.field1, b.field2, b.field3 FROM my_model b JOIN to_compare c ON (c.field1 = b.field1)

Join on a CTE in SQLAlchemy

自作多情 提交于 2020-12-15 06:41:30
问题 I'm trying to formulate a SQLAlchemy query that uses a CTE to build a table-like structure of an input list of tuples, and JOIN it with one of my tables (backend DB is Postgres). Conceptually, it would look like: WITH to_compare AS ( SELECT * FROM ( VALUES (1, 'flimflam'), (2, 'fimblefamble'), (3, 'pigglywiggly'), (4, 'beepboop') -- repeat for a couple dozen or hundred rows ) AS t (field1, field2) ) SELECT b.field1, b.field2, b.field3 FROM my_model b JOIN to_compare c ON (c.field1 = b.field1)