Peewee的基本使用
背景:刚接触大数据工作,要核对大量的接口返回数据的准确性验证工作,由于数据字典在编写sql的时候查看很不方便,而且对现有数据接口不是很清楚且表、字段数据量多且短时间内无法烂熟于心,给予此背景,简单的整理了一下peewee在工作中的应用。
前提:需先安装peewee/pwiz:
pip3 install peewee
pip3 install pwiz
1、环境具备了之后,在使用peewee库的时候要先生成model对象,此处对orm要有基本的了解;
python3 -m pwiz -e mysql -H host地址 -p 数据库端口 -u 用户名 -P 密码 -t 表名 库名 > 生成的orm_model文件名.py
生成model对象如下例子:
from peewee import *database = MySQLDatabase('qk_aic', **{'charset': 'utf8', 'sql_mode': 'PIPES_AS_CONCAT', 'use_unicode': True, 'host': '172.0.0.1', 'port': 3306, 'user': '123', 'password': '123'})class UnknownField(object):def __init__(self, *_, **__): passclass BaseModel(Model):class Meta:database = databaseclass Company(BaseModel):entname = CharField(column_name='ENTNAME', index=True)nacaoid = CharField(column_name='NACAOID', index=True)regno = CharField(column_name='REGNO', index=True)uniscid = CharField(column_name='UNISCID', index=True)entid = AutoField()updated = DateTimeField(constraints=[SQL("DEFAULT CURRENT_TIMESTAMP")], index=True)class Meta:table_name = 'company'
2、接下来生成model了 ,我们就可以进行下一步的查询操作了;
- 单条查询:
get:# 根据主键直接获取(注:主键搜索不存在的时候会报错) res = Company.get_by_id(279784) print(res) # 279784 res = Company[279784] print(res) # 279784# 根据字段名直接查询 res = Company.get(Company.entid == '279784')print(res) # 这里返回的是主键对应的值 print(res.entname, res.nacaoid, res.regno, res.uniscid, res.entid, res.updated) # 取其他值的时候直接使用"对象.字段名称"的方法获取值,没有值的时候返回' '
- 查询多条数据:
select:# 若select里面不指定字段,则查询的是全部字段 res = CompanyBasic.select().where(CompanyBasic.enttype == 1100) # 上面返回的'peewee.ModelSelect'对象存在惰性,不会立即执行,可以理解成迭代器,需要触发调用,此对象直接调用仍然只打印主键 # for i in res: # print(i.entid, i.abuitem) print(list(res)) # [<CompanyBasic: 279624>, <CompanyBasic: 279632>, <CompanyBasic: 279688>, <CompanyBasic: 279712>, ......] 因此可以将表.select().where()这种方式返回的对象可以当做列表来处理# 可以将上述获取的多条对象直接转换成字典 res = CompanyBasic.select().where(CompanyBasic.enttype == 1100).dicts() print(type(res)) # <class 'peewee.ModelSelect'> print(list(res)) # [{'entid': 279624, 'abuitem': None, 'ancheyear': None, 'apprdate': datetime.date(1999, 6, 7), 'candate': None, 'cbuitem': None, 'dom': '北京市崇文区前门东大街10号楼1004室',....}]# 将上述多条对象转换成元组 res = CompanyBasic.select().where(CompanyBasic.enttype == 1100).tuples() print(list(res))# 为查询字段起别名,起完别名只能使用别名去查询,使用alias() 方法 res = CompanyBasic.select(CompanyBasic.enttype.alias("企业类型"), CompanyBasic.opscope.alias('asdas')).where(CompanyBasic.enttype == 1100).dicts() # [{'企业类型': '1100', 'asdas': '文化交流;养殖业新技术开发;农业技术开发、咨询、转让和培训;仪器仪表的...}...]# 查找为NULL的字段,使用is_null()方法 res_not_null = CompanyBasic.select().where(CompanyBasic.opscope.is_null(False)) print(len(res_not_null)) # 1987 res_is_null = CompanyBasic.select().where(CompanyBasic.opscope.is_null()) # 默认True print(len(res_is_null)) # 13# 查询包含某个字符串,contains()方法 res = CompanyBasic.select().where(CompanyBasic.opscope.contains("文化交流")).dicts() print(list(res)) # info = [i.opscope for i in res] # print(info)# 查询包含某个字符串,contains()方法 res = CompanyBasic.select().where(CompanyBasic.opscope.contains("文化交流")).dicts() print(list(res)) info = [i.opscope for i in res] print(info)# 查询以指定关键字字符串开始 startswith() res = CompanyBasic.select(CompanyBasic.entid, CompanyBasic.opscope).where(CompanyBasic.opscope.startswith('文化')).dicts() print(list(res)) # [<CompanyBasic: 279624>, <CompanyBasic: 281512>, <CompanyBasic: 283472>, <CompanyBasic: 283544>]# 查询以指定关键字字符串结尾 endswith res = CompanyBasic.select(CompanyBasic.entid, CompanyBasic.opscope).where(CompanyBasic.opscope.endswith('咨询')).dicts() print(list(res))# 查询位于两个值之间的值 between() res = Company.select(Company.entid, Company.entname).where(Company.entid.between(279623, 279668)) for i in res:print(i.entid, i.entname)# 279623 中国华瑞投资控股有限公司# 在多个数据中查in_.() 注意里面要传list[],not_in作用与之相反 res = Company.select(Company.entid, Company.entname).where(Company.entid.in_([279623, 279624])).dicts() print(list(res)) # [{'entid': 279623, 'entname': '中国华瑞投资控股有限公司'}, {'entid': 279624, 'entname': '中青农村经济文化发展中心有限公司'}]# 在多个数据中查in_.() 注意里面要传list[],not_in作用与之相反 res = Company.select(Company.entid, Company.entname).where(Company.entid.in_([279623, 279624])).dicts() print(list(res))# 使用正则匹配可以使用 regexp()和iregexp()方法,前者要求大消息,后者不要求大小写
- group by 和 having
res = CompanyBasic.select(CompanyBasic.entid, CompanyBasic.enttype).where(CompanyBasic.entid).group_by(CompanyBasic.enttype).dicts() print(list(res)) # [{'entid': 283016, 'enttype': '10000'}, {'entid': 279624, 'enttype': '1100'}, {'entid': 279704, 'enttype': '1110'}]res = CompanyBasic.select(CompanyBasic.entid, CompanyBasic.enttype).where(CompanyBasic.entid).group_by(CompanyBasic.enttype).having(CompanyBasic.enttype == '1100').dicts() print(list(res)) # [{'entid': 279624, 'enttype': '1100'}]
Peewee的基本使用
背景:刚接触大数据工作,要核对大量的接口返回数据的准确性验证工作,由于数据字典在编写sql的时候查看很不方便,而且对现有数据接口不是很清楚且表、字段数据量多且短时间内无法烂熟于心,给予此背景,简单的整理了一下peewee在工作中的应用。
前提:需先安装peewee/pwiz:
pip3 install peewee
pip3 install pwiz
1、环境具备了之后,在使用peewee库的时候要先生成model对象,此处对orm要有基本的了解;
python3 -m pwiz -e mysql -H host地址 -p 数据库端口 -u 用户名 -P 密码 -t 表名 库名 > 生成的orm_model文件名.py
生成model对象如下例子:
from peewee import *database = MySQLDatabase('qk_aic', **{'charset': 'utf8', 'sql_mode': 'PIPES_AS_CONCAT', 'use_unicode': True, 'host': '172.0.0.1', 'port': 3306, 'user': '123', 'password': '123'})class UnknownField(object):def __init__(self, *_, **__): passclass BaseModel(Model):class Meta:database = databaseclass Company(BaseModel):entname = CharField(column_name='ENTNAME', index=True)nacaoid = CharField(column_name='NACAOID', index=True)regno = CharField(column_name='REGNO', index=True)uniscid = CharField(column_name='UNISCID', index=True)entid = AutoField()updated = DateTimeField(constraints=[SQL("DEFAULT CURRENT_TIMESTAMP")], index=True)class Meta:table_name = 'company'
2、接下来生成model了 ,我们就可以进行下一步的查询操作了;
- 单条查询:
get:# 根据主键直接获取(注:主键搜索不存在的时候会报错) res = Company.get_by_id(279784) print(res) # 279784 res = Company[279784] print(res) # 279784# 根据字段名直接查询 res = Company.get(Company.entid == '279784')print(res) # 这里返回的是主键对应的值 print(res.entname, res.nacaoid, res.regno, res.uniscid, res.entid, res.updated) # 取其他值的时候直接使用"对象.字段名称"的方法获取值,没有值的时候返回' '
- 查询多条数据:
select:# 若select里面不指定字段,则查询的是全部字段 res = CompanyBasic.select().where(CompanyBasic.enttype == 1100) # 上面返回的'peewee.ModelSelect'对象存在惰性,不会立即执行,可以理解成迭代器,需要触发调用,此对象直接调用仍然只打印主键 # for i in res: # print(i.entid, i.abuitem) print(list(res)) # [<CompanyBasic: 279624>, <CompanyBasic: 279632>, <CompanyBasic: 279688>, <CompanyBasic: 279712>, ......] 因此可以将表.select().where()这种方式返回的对象可以当做列表来处理# 可以将上述获取的多条对象直接转换成字典 res = CompanyBasic.select().where(CompanyBasic.enttype == 1100).dicts() print(type(res)) # <class 'peewee.ModelSelect'> print(list(res)) # [{'entid': 279624, 'abuitem': None, 'ancheyear': None, 'apprdate': datetime.date(1999, 6, 7), 'candate': None, 'cbuitem': None, 'dom': '北京市崇文区前门东大街10号楼1004室',....}]# 将上述多条对象转换成元组 res = CompanyBasic.select().where(CompanyBasic.enttype == 1100).tuples() print(list(res))# 为查询字段起别名,起完别名只能使用别名去查询,使用alias() 方法 res = CompanyBasic.select(CompanyBasic.enttype.alias("企业类型"), CompanyBasic.opscope.alias('asdas')).where(CompanyBasic.enttype == 1100).dicts() # [{'企业类型': '1100', 'asdas': '文化交流;养殖业新技术开发;农业技术开发、咨询、转让和培训;仪器仪表的...}...]# 查找为NULL的字段,使用is_null()方法 res_not_null = CompanyBasic.select().where(CompanyBasic.opscope.is_null(False)) print(len(res_not_null)) # 1987 res_is_null = CompanyBasic.select().where(CompanyBasic.opscope.is_null()) # 默认True print(len(res_is_null)) # 13# 查询包含某个字符串,contains()方法 res = CompanyBasic.select().where(CompanyBasic.opscope.contains("文化交流")).dicts() print(list(res)) # info = [i.opscope for i in res] # print(info)# 查询包含某个字符串,contains()方法 res = CompanyBasic.select().where(CompanyBasic.opscope.contains("文化交流")).dicts() print(list(res)) info = [i.opscope for i in res] print(info)# 查询以指定关键字字符串开始 startswith() res = CompanyBasic.select(CompanyBasic.entid, CompanyBasic.opscope).where(CompanyBasic.opscope.startswith('文化')).dicts() print(list(res)) # [<CompanyBasic: 279624>, <CompanyBasic: 281512>, <CompanyBasic: 283472>, <CompanyBasic: 283544>]# 查询以指定关键字字符串结尾 endswith res = CompanyBasic.select(CompanyBasic.entid, CompanyBasic.opscope).where(CompanyBasic.opscope.endswith('咨询')).dicts() print(list(res))# 查询位于两个值之间的值 between() res = Company.select(Company.entid, Company.entname).where(Company.entid.between(279623, 279668)) for i in res:print(i.entid, i.entname)# 279623 中国华瑞投资控股有限公司# 在多个数据中查in_.() 注意里面要传list[],not_in作用与之相反 res = Company.select(Company.entid, Company.entname).where(Company.entid.in_([279623, 279624])).dicts() print(list(res)) # [{'entid': 279623, 'entname': '中国华瑞投资控股有限公司'}, {'entid': 279624, 'entname': '中青农村经济文化发展中心有限公司'}]# 在多个数据中查in_.() 注意里面要传list[],not_in作用与之相反 res = Company.select(Company.entid, Company.entname).where(Company.entid.in_([279623, 279624])).dicts() print(list(res))# 使用正则匹配可以使用 regexp()和iregexp()方法,前者要求大消息,后者不要求大小写
- group by 和 having
res = CompanyBasic.select(CompanyBasic.entid, CompanyBasic.enttype).where(CompanyBasic.entid).group_by(CompanyBasic.enttype).dicts() print(list(res)) # [{'entid': 283016, 'enttype': '10000'}, {'entid': 279624, 'enttype': '1100'}, {'entid': 279704, 'enttype': '1110'}]res = CompanyBasic.select(CompanyBasic.entid, CompanyBasic.enttype).where(CompanyBasic.entid).group_by(CompanyBasic.enttype).having(CompanyBasic.enttype == '1100').dicts() print(list(res)) # [{'entid': 279624, 'enttype': '1100'}]