45 - ORM框架知多少
1. 在python语言中有哪些常用的ORM框架,它们有什么区别
ORM(Object Ralational Mapping, 对象关系映射) 用来把对象模型表示的对象映射到基于SQL的关系型数据库结构中去
SQLAlchemy:偏向于SQL,可以灵活地提交SQL语句
SQLObject:更加面向对象,无法自由使用原生的SQL语句
2. 如何使用SQLObject框架操作MySQL数据库
''' pip install sqlobject ''' from sqlobject import * from sqlobject.mysql import builder import json mysql = 'mysql://root:root@localhost:3306/db?charset=utf8' sqlhub.processConnection = connectionForURI(mysql, driver='pymysql') class Person(SQLObject): class sqlmeta: table = 't_persons' name = StringCol(length = 30) age = IntCol() address = StringCol(length = 30) salary = FloatCol() try: Person.dropTable() except: pass Person.createTable() print('create success') # 插入记录 person1 = Person(name = 'Bill', age = 15, address = 'China', salary = 1111) person2 = Person(name = 'Mike', age = 20, address = 'beijing', salary = 2222) person3 = Person(name = 'John', age = 49, address = 'shanghai', salary = 3333) print('insert success')
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
create success insert success
1
2
# 修改 person2.name = 'laowang' person2.address = 'shanghai'
1
2
3
# 查询数据表 persons = Person.selectBy(name = 'Bill') print(persons[0]) print(persons[0].id) print(persons[0].name) print(persons[0].address) print(persons[0].salary)
1
2
3
4
5
6
7
1
2
3
4
5
# 删除数据 persons[0].destroySelf() print('del success')
1
2
3
del success
1
46 - 将xml文档保存在MongoDB数据库中
SQL
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。