注意:修改数据库 一定要跟着session.commit()
create table(往数据库增加表信息)
new_player = pt(player_name = 'player1',sex='male')
session.add(new_player)
session.commit()
query(表名)
query(表名.列名) <=> select 列名 from 表名
---返回的是对象
item_class = session.query(treasure).filter(treasure.ts_name=='maybe').one()
item_class.ts_class --用.去访问字段
--output
'tool '
---则返回的可以说是一个list?一个tuple一样的东西
item_class = session.query(treasure.ts_class).filter(treasure.ts_name=='maybe').first()
item_class[0] --和list/tuple取值的方法一样,下标取值
--output
'tool '
同时 query+first也可以用于判断是否存在所要查询的值,若不存在返回None。(query+count也可以实现判断是否存在所要查询的值的判断,若不存在则返回0)
--也就是将方法二直接合并了,这样的话item_class就直接去到了字符串
--返回字符串
item_class = session.query(treasure.ts_class).filter(treasure.ts_name=='maybe').first()[0]
--output
'tool '
4.方法四:query+all()
--查询一个玩家的所有treasure
--返回list
ts_list = session.query(pt.ts_name).filter(pt.player_name==player_name).all()
query(表名)
query(表名.列名) <=> select 列名 from 表名
--将player表中特定的玩家的find_or_not改为false
session.query(player)
.filter(player.player_name == player_name)
.update({'sex': 'male'})
--多值一起修改
session.query(player)
.filter(player.player_name == 'player1')
.update({'player_money':10,'login_at':time.time()})
session.commit() --只有了commit()了数据库才真正修改
(我现在有点怀疑.filter(player.player_name)
可以用.filter('player_name')
代替)
和sql有少许不同,在sql中对于单表的查询
select player_name from player where player_name = 'player1';
没有任何歧义,但是在sqlalchemy中,则可能会发生意想不到的错误(这是我找了4个小时的bug),所以在查询语句中一定要明确查询的表名,尽管只是单表查询filter(player.player_name==‘player1’)
--返回list
a=session.query(pt.ts_name)
.join(treasure)
.filter(treasure.ts_class=='luck',pt.player_name=='player1').all()
---删除pt表中ts_name是drop_ts_name的这一行数据
session.query(pt).filter(pt.ts_name==drop_ts_name,player_name==player_name).delete()
drop database game;
#output
ERROR: database "game" is being accessed by other users
DETAIL: There is 1 other session using the database.
解决方法:
# 先执行:
select pg_terminate_backend(pid) from pg_stat_activity where DATNAME = 'game';
#output
pg_terminate_backend
----------------------
t
(1 row)
# 再执行
drop database game;
(Background on this error at: http://sqlalche.me/e/f405)
看博客有两个原因:
sqlalchemy.exc.ProgrammingError: (mysql.connector.errors.ProgrammingError) 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) (Background on this error at: http://sqlalche.me/e/f405)
则需要重新设置密码:(可参考)https://blog.csdn.net/tao01230/article/details/46410525
sql的schema中写的是on_sale_or_not是smallint类型,后来在py文件里,写的class
class treasure(Base):
__tablename__ = 'treasure'
ts_name = Column(String(20),primary_key=True)
on_sale_or_not = Column(Boolean)
own_or_not = Column(Boolean)
wear_or_not = Column(Boolean)
中on_sale_or_not之类的写的类型为boolean所以导致了,数据类型不匹配而出现的错误
date boolean这种数据类型找不到,要从
from sqlalchemy import Column,String,Integer,Boolean,Time,ForeignKey,create_engine,PrimaryKeyConstraint
import 数据类型
sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped entity expected - got '<__main__.player object at 0x10b4be7f0>
在执行如下语句时:
session.query(player).filter(player.player_name=='cyy').one()
解决方案:重新建player表(因为真的疯了 对于另外的表就可以访问,这个就弄死不行,我就重新建表了,没想到还好使)
(这也是折磨我疯了的一个问题)
####解决方法:
查询博客/文档发现
post/get参考链接:https://www.cnblogs.com/logsharing/p/8448446.html
pytest-cov参考链接:https://pypi.org/project/pytest-cov/2.0.0/