1.peewee支持的运算符
2.peewee支持的方法
3.pewee支持的逻辑运算符
4.通过类名的方法直接获取数据测试代码展现
def query_data1():
m = Music.get(Music.id == 2)
print(f"Music.get========={m}")
print(f"Music.get========={m.singer}")
m = Music.get_by_id(2)
print(f"Music.get_by_id========={m}")
print(f"Music.get_by_id========={m.singer}")
m = Music[2]
print(f"Music[2]========={m}")
print(f"Music[2]========={m.singer}")
'''
Music.get=========2
Music.get=========薛之谦
Music.get_by_id=========2
Music.get_by_id=========薛之谦
Music[2]=========2
Music[2]=========薛之谦
'''
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
5.通过SELECT方法查询代码展示
def query_by_select():
tmp_ms = Music.select().execute()
return tmp_ms
'''
1===你是我左边的风景===林志炫===0
2===把你揉碎捏成苹果===薛之谦===0
3===游戏人间===童安格===0
4===故乡的云===费翔===0
5===诺言Jason===青城山下白素贞===0
6===勇敢的幸福===Sweety===0
7===爱丫爱丫===By2===0
8===我也曾像你一样===马天宇===0
'''
if __name__ == '__main__':
tmp = query_by_select()
for m in tmp:
print(f'{m.id}==={m.name}==={m.singer}==={m._type}')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
6.过滤列,通过select方法中增加列名
def query_by_select_field():
tmp_ms = Music.select(Music.id,Music.name,Music.singer,Music._type).execute()
return tmp_ms
'''
1===你是我左边的风景===林志炫===0
2===把你揉碎捏成苹果===薛之谦===0
3===游戏人间===童安格===0
4===故乡的云===费翔===0
5===诺言Jason===青城山下白素贞===0
6===勇敢的幸福===Sweety===0
7===爱丫爱丫===By2===0
8===我也曾像你一样===马天宇===0
'''
7.过滤数据,通过where单个条件
def query_by_select_data1():
tmp_ms = Music.select().where(Music.id>5)
return tmp_ms
'''
6===勇敢的幸福===Sweety===0
7===爱丫爱丫===By2===0
8===我也曾像你一样===马天宇===0
'''
8.过滤数据,通过where多个条件and
def query_by_select_data2():
tmp_ms = Music.select().where((Music.id>5) & (Music.singer == 'By2'))
return tmp_ms
'''
7===爱丫爱丫===By2===0
'''
9.过滤数据,通过where多个条件or
def query_by_select_data3():
tmp_ms = Music.select().where((Music.id>5) | (Music.singer == '林志炫'))
return tmp_ms
'''
1===你是我左边的风景===林志炫===0
6===勇敢的幸福===Sweety===0
7===爱丫爱丫===By2===0
8===我也曾像你一样===马天宇===0
'''
10.过滤数据,模糊查询
def query_by_select_like():
tmp_ms = Music.select().where(Music.name.contains('你'))
return tmp_ms
'''
1===你是我左边的风景===林志炫===0
2===把你揉碎捏成苹果===薛之谦===0
8===我也曾像你一样===马天宇===0
'''
11.过滤数据,排序
def query_by_select_like():
tmp_ms = Music.select().order_by(Music.id)
tmp_ms = Music.select().order_by(Music.id.asc())
tmp_ms = Music.select().order_by(+Music.id)
tmp_ms = Music.select().order_by(Music.id.desc())
tmp_ms = Music.select().order_by(-Music.id)
return tmp_ms
12.处理数据,分页
def query_by_select_page():
tmp_ms = Music.select().paginate(1,3)
return tmp_ms
'''
1===你是我左边的风景===林志炫===0
2===把你揉碎捏成苹果===薛之谦===0
3===游戏人间===童安格===0
'''
13.完整代码
from peewee import *
from peewee import database_required
db = MySQLDatabase('tornado_db', host='127.0.0.1', port=3306, user='root',passwd='root')
class BaseModel(Model):
class Meta:
database = db
class Commany(BaseModel):
name = CharField(verbose_name='公司名称')
full_name = CharField(verbose_name='公司全名称')
year = IntegerField(verbose_name='开业时间')
class Meta:
table_name = 't_commany'
class Music(BaseModel):
name = CharField(verbose_name='音乐名称')
singer = CharField(verbose_name='演唱者')
duration = IntegerField(verbose_name='时长')
_type = IntegerField(verbose_name='类型')
commany = ForeignKeyField(Commany, verbose_name='版权所属',backref='musics')
class Meta:
table_name = 't_music'
def init_table():
db.create_tables([Commany,Music])
def query_data1():
m = Music.get(Music.id == 2)
print(f"Music.get========={m}")
print(f"Music.get========={m.singer}")
m = Music.get_by_id(2)
print(f"Music.get_by_id========={m}")
print(f"Music.get_by_id========={m.singer}")
m = Music[2]
print(f"Music[2]========={m}")
print(f"Music[2]========={m.singer}")
'''
Music.get=========2
Music.get=========薛之谦
Music.get_by_id=========2
Music.get_by_id=========薛之谦
Music[2]=========2
Music[2]=========薛之谦
'''
def query_by_select():
tmp_ms = Music.select().execute()
return tmp_ms
'''
1===你是我左边的风景===林志炫===0
2===把你揉碎捏成苹果===薛之谦===0
3===游戏人间===童安格===0
4===故乡的云===费翔===0
5===诺言Jason===青城山下白素贞===0
6===勇敢的幸福===Sweety===0
7===爱丫爱丫===By2===0
8===我也曾像你一样===马天宇===0
'''
def query_by_select_field():
tmp_ms = Music.select(Music.id,Music.name,Music.singer,Music._type).execute()
return tmp_ms
'''
1===你是我左边的风景===林志炫===0
2===把你揉碎捏成苹果===薛之谦===0
3===游戏人间===童安格===0
4===故乡的云===费翔===0
5===诺言Jason===青城山下白素贞===0
6===勇敢的幸福===Sweety===0
7===爱丫爱丫===By2===0
8===我也曾像你一样===马天宇===0
'''
def query_by_select_data1():
tmp_ms = Music.select().where(Music.id>5)
return tmp_ms
'''
6===勇敢的幸福===Sweety===0
7===爱丫爱丫===By2===0
8===我也曾像你一样===马天宇===0
'''
def query_by_select_data2():
tmp_ms = Music.select().where((Music.id>5) & (Music.singer == 'By2'))
return tmp_ms
'''
7===爱丫爱丫===By2===0
'''
def query_by_select_data3():
tmp_ms = Music.select().where((Music.id>5) | (Music.singer == '林志炫'))
return tmp_ms
'''
1===你是我左边的风景===林志炫===0
6===勇敢的幸福===Sweety===0
7===爱丫爱丫===By2===0
8===我也曾像你一样===马天宇===0
'''
def query_by_select_like():
tmp_ms = Music.select().where(Music.name.contains('你'))
return tmp_ms
'''
1===你是我左边的风景===林志炫===0
2===把你揉碎捏成苹果===薛之谦===0
8===我也曾像你一样===马天宇===0
'''
def query_by_select_order():
tmp_ms = Music.select().order_by(Music.id)
tmp_ms = Music.select().order_by(Music.id.asc())
tmp_ms = Music.select().order_by(+Music.id)
tmp_ms = Music.select().order_by(Music.id.desc())
tmp_ms = Music.select().order_by(-Music.id)
return tmp_ms
def query_by_select_page():
tmp_ms = Music.select().paginate(1,3)
return tmp_ms
'''
1===你是我左边的风景===林志炫===0
2===把你揉碎捏成苹果===薛之谦===0
3===游戏人间===童安格===0
'''
if __name__ == '__main__':
tmp = query_by_select()
tmp = query_by_select_field()
tmp = query_by_select_data1()
tmp = query_by_select_data2()
tmp = query_by_select_data3()
tmp = query_by_select_like()
tmp = query_by_select_order()
tmp = query_by_select_page()
for m in tmp:
print(f'{m.id}==={m.name}==={m.singer}==={m._type}')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181