标签:open gif 正是 key 创建数据库 add 的区别 orm 数据库名
好久没有更新Blog了
今天来聊一聊 Python 的 ORM 框架 SQLAlchemy 有的孩子已经听说过这个框架的大名了,也听说了 SQLAlchemy 没有 Django 的 Models 好用
我在这里辟谣一下, Models 紧紧只是配置和使用比较简单,因为他是Django自带的ORM框架,也正是因为是Django原生的,所以兼容性远远不如SQLAlchemy
真正算得上全面的ORM框架是我们的SQLAlchemy ORM框架,它可以在任何使用SQL查询时使用
当然了,无论是使用什么ORM框架,都是为了方便不熟练数据库的同学使用的,个人还是比较推崇原生 SQL 哈
ok,我们来看一下 SQLAlchemy 如何使用:
1.创建表的一波说不出来但很牛x的操作
#create_table.py
#通过SQLAlchemy创建数据表 # 1.导入SQLAlchemy,没安装的记得安装一下啊 from sqlalchemy.ext.declarative import declarative_base # 2.创建ORM模型基类 Base = declarative_base() # 相当于Django Model # 3.导入ORM对应数据库数据类型的字段 from sqlalchemy import Column, Integer, String # 4.创建ORM对象 class User(Base): __tablename__ = "user" id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(32), index=True) # 5.创建数据库连接 from sqlalchemy import create_engine #mysql+pymysql用mysql数据库+pymysql,root用户,614615数据库密码,@127.0.0.1本地地址,3306数据库端口号,led数据库名,剩下的是字符编码,这要是不懂,滚!!! engine = create_engine("mysql+pymysql://root:614615@127.0.0.1:3306/led?charset=utf8") # 数据库连接创建完成 # 6.去数据库中创建与User所对应的数据表 # 去engine数据库中创建所有继承Base类的 ORM对象 Base.metadata.create_all(engine)
2.增的操作
#curd_insert.py # 增 # 1.打开数据库的链接 from create_table import engine # 2.创建绘画 from sqlalchemy.orm import sessionmaker # 3.创建会话的窗口 Session = sessionmaker(engine) # 4.打开会话窗口 db_session = Session() from create_table import User with open(‘name.txt‘,‘r‘)as f: for i in f.read(): db_session.add_all([ User(name=i), ]) db_session.commit() db_session.close() #这里说一下,add和add_all的区别,就是单挑和多条的差距,当然,add_all也可以插入单条,只不过消耗的资源多一些,
3.改的操作
#curd_update.py # 更新 from sqlalchemy.orm import sessionmaker from create_table import User,engine Session = sessionmaker(engine) db_session = Session() ret = db_session.query(User).filter(User.name == "李志强").update({"name":"好人"}) db_session.commit()#切记!!执行语句 db_session.close()#关闭会话 ret = db_session.query(User).filter(User.id >=60).update({"name":"我是好人"}) db_session.commit() db_session.close()
4.查的操作
#curd_select.py #查 from create_table import engine from create_table import User from sqlalchemy.orm import sessionmaker Session = sessionmaker(engine) db_session = Session() # 1.简单的查询 res = db_session.query(User).all() for i in res: print(i.name) res_list = db_session.query(User).first() print(res_list.id) # 2.有条件的查询 ret = db_session.query(User).filter(User.name == "李二短").first() # 因为这里查到一个所以不会用索引取值 print(ret.name,ret.id) ret = db_session.query(User).filter(User.name == "浩").all() # 这里查到所有所以会用索引取值第一个 print(ret[0].name,ret[0].id) ret = db_session.query(User).filter(User.id >= 60).all() # 这里说一下,查询所有的id>=60的,查询所有进行遍历取值 for i in ret: print(i.name,i.id) # 查看原生的sql语句办法 ret = db_session.query(User).filter(User.id >= 60) print(ret)
5.删除的操作
curd_delete.py # 删除 from sqlalchemy.orm import sessionmaker from create_table import engine,User Session = sessionmaker(engine) db_session = Session() ret = db_session.query(User).filter(User.name == "我是好人").delete() db_session.commit() db_session.close() # print(ret) ret = db_session.query(User).filter(User.id >= 20).delete() db_session.commit() db_session.close() # print(ret)
二.一对多的表关系操作ForeginKey
1.创建一对多的表关系
#create_table_ForeginKey.py # 一对多建表操作 from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() from sqlalchemy import create_engine from sqlalchemy import Column,Integer,String,ForeignKey from sqlalchemy.orm import relationship class School(Base): __tablename__ = ‘school‘ id = Column(Integer,primary_key=True) name = Column(String(32)) class Student(Base): __tablename__ = ‘student‘ id = Column(Integer,primary_key=True) name = Column(String(32)) school_id = Column(Integer,ForeignKey("school.id")) stu2sch = relationship("School",backref="stu2sch") engine = create_engine("mysql+pymysql://root:614615@127.0.0.1:3306/led?charset=utf8") Base.metadata.create_all(engine)
2.增加数据
#curd_insert_ForeginKey.py #添加数据 from sqlalchemy.orm import sessionmaker from create_table_ForeignKey import Student,School,String,Integer,engine Session = sessionmaker(engine) db_session = Session() # 正向的添加数据 relationship版本 sch_obj = School(name="清华") sch_obj.stu2sch = [Student(name="李志强"),Student(name="李二短")] db_session.add(sch_obj) db_session.commit() db_session.close() #反向的添加数据 relationship版本 stu_obj = Student(name="龙龙",stu2sch=School(name="北大")) db_session.add(stu_obj) db_session.commit() db_session.close()
3.修改操作
curd_update_ForeginKey.py from sqlalchemy.orm import sessionmaker from create_table_ForeignKey import School,Student,String,Integer,engine Session = sessionmaker(engine) db_session = Session() sch = db_session.query(School).filter(School.name == "清华").first() # 查到学校的id,在学生表里查到学生是龙龙的学生把学校id改成察到的学校id db_session.query(Student).filter(Student.name == "龙龙").update({"school_id":sch.id}) db_session.commit() db_session.close()
4.查的操作
#curd_select_ForeginKey.py from sqlalchemy.orm import sessionmaker from create_table_ForeignKey import School,Student,engine,String,Integer Session = sessionmaker(engine) db_session = Session() #relationship正向查询版本 stu = db_session.query(Student).all() for i in stu: print(i.name,i.id,i.stu2sch.name) # relationship反向查询版本 sch = db_session.query(School).all() for school in sch: for student in school.stu2sch: print(school.id,school.name,student.name)
5.delete
#curd_delete_ForeginKey.py from sqlalchemy.orm import sessionmaker from create_table_ForeignKey import Student,School,String,Integer,engine Session = sessionmaker(engine) db_session = Session() sch = db_session.query(School).filter(School.name == ‘北大‘).first() print(sch.id) db_session.query(Student).filter(Student.school_id == sch.id).delete() db_session.commit() db_session.close()
三。多对多表的操作
未完,待续
标签:open gif 正是 key 创建数据库 add 的区别 orm 数据库名
原文地址:https://www.cnblogs.com/lzqrkn/p/10376603.html