Python SQLAlchemy簡(jiǎn)介及基本用法
SQLAlchemy庫(kù)簡(jiǎn)單介紹
SQLAlchemy是一個(gè)基于Python實(shí)現(xiàn)的ORM對(duì)象關(guān)系映射框架。
該框架建立在DB API之上,使用關(guān)系對(duì)象映射進(jìn)行數(shù)據(jù)庫(kù)操作,將類和對(duì)象轉(zhuǎn)換成SQL,然后使用數(shù)據(jù)API執(zhí)行SQL并獲取執(zhí)行結(jié)果。
SQLAlchemy的下載安裝
// 默認(rèn)從官網(wǎng)下載安裝PyMySQL庫(kù) pip3 install sqlalchemy // 從豆瓣源下載安裝PyMySQL庫(kù) pip3 install sqlalchemy -i https://pypi.douban.com/simple // 從清華源下載安裝PyMySQL庫(kù) pip3 install sqlalchemy -i https://pypi.tuna.tsinghua.edu.cn/simple
SQLAlchemy的組件構(gòu)成
SQLAlchemy ORM組成部分如下: Object Relation Mapping(ORM):對(duì)象關(guān)系映射
SQLAlchemy Core組成部分如下: Engine:框架的引擎 Connection Pooling:數(shù)據(jù)庫(kù)連接池 Dialect:選擇連接數(shù)據(jù)庫(kù)的DB API種類 Schema/Types:架構(gòu)和類型 SQL Exprression Language:SQL表達(dá)式語(yǔ)言
SQLAlchemy本身無(wú)法操作數(shù)據(jù)庫(kù),其必須以來(lái)PYMYSQL等第三方插件驅(qū)動(dòng), Dialect用于和數(shù)據(jù)API進(jìn)行交流,根據(jù)配置文件的不同調(diào)用不同的數(shù)據(jù)庫(kù)API, 從而實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的操作
MySQL-Python
mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname>
pymysql
mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
MySQL-Connector
mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
cx_Oracle
oracle+cx_oracle://user:pass@host:port/dbname[?key=value&key=value...]
更多:http://docs.sqlalchemy.org/en/latest/dialects/index.html配置數(shù)據(jù)庫(kù)連接串
uri: dialect[+driver]://user:password@host/dbname[?key=value..] - dialect:數(shù)據(jù)庫(kù),如:sqlite、mysql、oracle、postgresql等 - driver:數(shù)據(jù)庫(kù)驅(qū)動(dòng),用于連接數(shù)據(jù)庫(kù),比如pymysql、mysqldb等 - username:數(shù)據(jù)庫(kù)用戶 - password:數(shù)據(jù)庫(kù)密碼 - host:數(shù)據(jù)庫(kù)服務(wù)IP地址 - port:數(shù)據(jù)庫(kù)服務(wù)端口 - database:數(shù)據(jù)庫(kù)名
# 實(shí)例:MySQL + PyMySQL
# MySQL服務(wù)端配置信息
DB_INFO = dict(
host="127.0.0.1",
port=6379,
user="admin",
password="123456",
database="test",
charset="utf8"
)
# 數(shù)據(jù)庫(kù)連接URL格式化
DB_URI = 'mysql+pymysql://{user}:{password}@{host}:{port}/{database}?charset={charset}'.format(**DB_INFO)創(chuàng)建引擎并連接數(shù)據(jù)庫(kù)
engine = create_engine("mysql+pymysql://root:123456@localhost:3306/db4?charset=utf8",
max_overflow=0, # 超過(guò)連接池大小外最多創(chuàng)建的連接
pool_size=5, # 連接池大小
pool_timeout=30, # 池中沒(méi)有線程最多等待的時(shí)間,否則報(bào)錯(cuò)
pool_recycle=-1 # 多久之后對(duì)線程池中的線程進(jìn)行一次連接的回收(重置)
echo = True # echo參數(shù)為True時(shí),會(huì)顯示每條執(zhí)行的SQL語(yǔ)句,可以關(guān)閉 ",max_overflow = 5)from sqlalchemy import create_engine
# 連接地址
DB_URI = 'mysql+pymysql://{user}:{password}@{host}:{port}/{database}?charset={charset}'
# 創(chuàng)建引擎
engine = create_engine(DB_URI)
# 打開(kāi)連接
conn = engine.connect()
# 執(zhí)行查詢
result = conn.execute('select * from user limit %s offset %s', 10, 2)
# 獲取單條數(shù)據(jù)
data_line = result.fetchone()
# 獲取多條數(shù)據(jù)
data_list = result.fetchmany(2)
# 獲取全部數(shù)據(jù)
data_list = result.fetchall()
# 插入數(shù)據(jù)操作,獲取最后行ID
last_row_id = result.lastrowid
# 關(guān)閉連接
conn.close() 數(shù)據(jù)庫(kù)對(duì)象映射模型
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
# 數(shù)據(jù)庫(kù)連接地址
DB_URI = 'mysql+pymysql://{user}:{password}@{host}:{port}/{database}?charset={charset}'
# 創(chuàng)建數(shù)據(jù)庫(kù)引擎
engine = create_engine(DB_URI)
# 模型基類
Base = declarative_base(engine)
session = sessionmaker(engine)()
class Student(Base):
"""功能:學(xué)生映射模型類"""
__tablename__ = 'Student'
id = Column(Integer, primary_key=True, autoincrement=True, comment="主鍵ID")
name = Column(String(50), index=True, nullable=True, comment="學(xué)生名稱")
age = Column(Integer, comment="學(xué)生年齡")
sex = Column(String(10), comment="學(xué)生性別")
# 創(chuàng)建全部表,默認(rèn)自動(dòng)跳過(guò)已存在表
Base.metadata.create_all()
# 創(chuàng)建指定表,默認(rèn)自動(dòng)跳過(guò)已存在表
Base.metadata.create_all(tables=[Student.__table__])
# 刪除全部表,默認(rèn)自動(dòng)跳過(guò)不存在的表
Base.metadata.drop_all()
# 刪除指定表,默認(rèn)自動(dòng)跳過(guò)不存在的表
Base.metadata.drop_all(tables=[Student.__table__])到此這篇關(guān)于SQLAlchemy簡(jiǎn)介以及基本使用的文章就介紹到這了,更多相關(guān)SQLAlchemy基本使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python?ORM框架之SQLAlchemy?的基礎(chǔ)用法
- Python?flask?sqlalchemy的簡(jiǎn)單使用及常用操作
- python flask sqlalchemy連接數(shù)據(jù)庫(kù)流程介紹
- python sqlalchemy動(dòng)態(tài)修改tablename兩種實(shí)現(xiàn)方式
- Python+SQLAlchemy輕松實(shí)現(xiàn)管理數(shù)據(jù)庫(kù)
- 3個(gè)Python?SQLAlchemy數(shù)據(jù)庫(kù)操作功能詳解
- Python使用SQLAlchemy模塊實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)
- Python?SQLAlchemy與數(shù)據(jù)庫(kù)交互操作完整指南
- Python?SQLAlchemy庫(kù)的實(shí)現(xiàn)示例
相關(guān)文章
python實(shí)現(xiàn)簡(jiǎn)易內(nèi)存監(jiān)控
這篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)易內(nèi)存監(jiān)控,每隔3秒獲取系統(tǒng)內(nèi)存,當(dāng)內(nèi)存超過(guò)設(shè)定的警報(bào)值時(shí),獲取所有進(jìn)程占用內(nèi)存并發(fā)出警報(bào)聲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
利用Python批量壓縮png方法實(shí)例(支持過(guò)濾個(gè)別文件與文件夾)
這篇文章主要給大家介紹了關(guān)于利用Python批量壓縮png的相關(guān)資料,文中介紹的方法支持過(guò)濾個(gè)別文件與文件夾,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面跟著小編來(lái)一起看看吧。2017-07-07
TensorFlow2.0使用keras訓(xùn)練模型的實(shí)現(xiàn)
這篇文章主要介紹了TensorFlow2.0使用keras訓(xùn)練模型的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
python中的代碼編碼格式轉(zhuǎn)換問(wèn)題
本文給大家講解的是使用Python實(shí)現(xiàn)代碼編碼格式轉(zhuǎn)換的問(wèn)題,十分的簡(jiǎn)單實(shí)用,有需要的小伙伴可以參考下。2015-06-06
Python tkinter布局與按鈕間距設(shè)置方式
這篇文章主要介紹了Python tkinter布局與按鈕間距設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03

