【什么是ORM】
ORM 全稱是(Object Relational Mapping)表示對(duì)象關(guān)系映射; 通俗理解可以理解為編程語言的虛擬數(shù)據(jù)庫;
【理解ORM】
用戶地址信息數(shù)據(jù)庫表與對(duì)象的映射
【ORM的重要特性】
1.面向?qū)ο蟮木幊趟枷?,方便擴(kuò)充
2. 少寫(幾乎不寫)sql,提升開發(fā)效率
3.支持多種類型的數(shù)據(jù)庫(常用的mysql,pg,oracle等等),方便切換
4.ORM技術(shù)已經(jīng)相當(dāng)成熟,能解決絕大部分問題
【ORM模型框架的選擇】
【SQLAlchemy ORM模型】
眾所周知,ORM框架模型可選擇的有很多,那么我們這邊選擇了SQLAlchemy 模型框架
pip install SQLAlchemy 安裝sql alchemy; 也可以指定版本號(hào)pip install SQLAlchemy ==1.4.17
import sqlalcherm; sqlalchemy.__version__; 驗(yàn)證是否安裝成功及版本號(hào);
【SQL Alchemy的使用】
一.開始連接數(shù)據(jù)庫
二.聲明ORM模型基類
三.實(shí)現(xiàn)ORM模型類
四.同步數(shù)據(jù)庫表
開始連接數(shù)據(jù)庫
- 延遲連接(Lazy Connecting)——只有在真正操作數(shù)據(jù)庫的時(shí)候,才會(huì)連接數(shù)據(jù)庫
- 連接代碼示例
from sqlalchemy import create_engine
create_engine("mysql://root:@127.0.0.1:3306/school?charset=utf8,echo=True,future=True")
create_engine 參數(shù)解釋
- url(默認(rèn)第一個(gè)參數(shù))——連接到哪種類型的數(shù)據(jù)庫,如:mysql;以哪種數(shù)據(jù)庫連接器(驅(qū)動(dòng))來連接數(shù)據(jù)庫
- echo——是否輸出logging(日志)信息,會(huì)把日志都打印出來
- future使用SQLAlchemy2.0 API風(fēng)格
SQLAlchemy配置
當(dāng)密碼中含有特殊字符時(shí),怎么處理?
話不多說,見下方代碼
from urllib.parse import quote_plus
如果密碼里有特殊字符時(shí),這邊需要導(dǎo)入一個(gè)類來處理
password_formatted= quote.plus("mima%mima")
把處理后的密碼粘貼到上方的sqlalchemy配置中,即可
聲明ORM模型基類
from sqlalchemy.orm import declarative_base
聲明這個(gè)基類
Base = declarative_base()
【實(shí)現(xiàn)ORM模型類】
如何實(shí)現(xiàn)? 我們需要寫1個(gè)類去繼承他
然后還需要設(shè)立1個(gè)屬性
from sqlalchemy import Column, Integer, String, DateTime
class Student(Base):
"""學(xué)生信息表"""
__tablename__ = 'student'
id = Column(Integer, name='id', primary_key=True)
stu_no = Column(Integer, nullable=False, comment='學(xué)號(hào)')
stu_name = Column(String(16), nullable=False, comment='姓名')
created_at = Column(DateTime)
1.需要在同步之前保證 數(shù)據(jù)庫中有這個(gè)庫,如果沒有,則需要手動(dòng)創(chuàng)建
2 創(chuàng)建表,刪除表
from orm_connect_example import Base ,engine
# 創(chuàng)建表
Base.metadata.create_all(engine)
#刪除表
Base.metadata.drop_all(engine)
【ORM對(duì)應(yīng)的模型字段類型】
【代碼示例】
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column, Integer, String, DateTime
# 第一步,準(zhǔn)備連接
engine = create_engine('mysql://root:@10.72.100.1:8081/test_database_1?charset=utf8',echo=True)
# 第二步,聲明ORM模型的基類
Base = declarative_base()
# 第三步,實(shí)現(xiàn)ORM模型類
class Student(Base):
"""學(xué)生信息表"""
__tablename__ = 'student'
id = Column(Integer, name='id', primary_key=True)
stu_no = Column(Integer, nullable=False, comment='學(xué)號(hào)')
stu_name = Column(String(16), nullable=False, comment='姓名')
created_at = Column(DateTime)
#第四步 同步數(shù)據(jù)庫表
def create_table()
"""同步數(shù)據(jù)庫表"""
# 新建表
Base.metadata.create_all(bind=engine)
# 刪除表
Base.metadata.drop_all(bind=engine)
到此這篇關(guān)于ORM模型框架操作mysql數(shù)據(jù)庫的方法的文章就介紹到這了,更多相關(guān)ORM模型框架內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Laravel框架Eloquent ORM簡介、模型建立及查詢數(shù)據(jù)操作詳解
- django框架面向?qū)ο驩RM模型繼承用法實(shí)例分析
- MySql用DATE_FORMAT截取DateTime字段的日期值
- mysql數(shù)據(jù)庫中的information_schema和mysql可以刪除嗎?