Python之ORM框架SQLAlchemy是什么-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、成都做網(wǎng)站、馬關(guān)網(wǎng)絡(luò)推廣、小程序開發(fā)、馬關(guān)網(wǎng)絡(luò)營銷、馬關(guān)企業(yè)策劃、馬關(guān)品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供馬關(guān)建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:muchs.cn

這篇文章主要介紹Python之ORM框架SQLAlchemy是什么,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

一、SQLAlchemy簡介

SQLAlchemy是Python SQL工具包和對象關(guān)系映射器,是python中最著名的ORM(Object Relationship Mapping)框架,它簡化了應(yīng)用程序開發(fā)人員在原生SQL上的操作,使開發(fā)人員將主要精力都放在程序邏輯上,從而提高開發(fā)效率。它提供了一整套著名的企業(yè)級持久性模式,設(shè)計用于高效和高性能的數(shù)據(jù)庫訪問。

使用ORM操作數(shù)據(jù)庫:

優(yōu)勢 :代碼易讀,隱藏底層原生SQL語句,提高了開發(fā)效率。

劣勢 :執(zhí)行效率低 ,將方法轉(zhuǎn)換為原生SQL后 原生SQL不一定是最優(yōu)的。

環(huán)境:

Windows 7  x64   python 3.7.1  SQLAlchemy 1.3.5   pymysql 0.9.3    mysqld  5.6.43

關(guān)于如何安裝上述環(huán)境,這里不做演示,相信大家能搜到這篇文章,想必這種小問題不在話下。

二、簡單的操作

1、準(zhǔn)備數(shù)據(jù)庫sql_demo

mysql> create database sql_demo;

2、在sql_demo中創(chuàng)建表

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
Base = declarative_base()  # 導(dǎo)出官方基礎(chǔ)類
class SQLDemo(Base):
    __tablename__ = 'SqlDemo'  # 表名
    # 創(chuàng)建字段
    id = Column(Integer,primary_key=True,autoincrement=True)
    name = Column(String(32))
# 創(chuàng)建數(shù)據(jù)庫引擎
engine = create_engine('mysql+pymysql://數(shù)據(jù)庫用戶:數(shù)據(jù)庫密碼@127.0.0.1:3306/數(shù)據(jù)庫名?charset=utf8')
# 創(chuàng)建表
Base.metadata.create_all(engine)

運行上面的代碼后會在數(shù)據(jù)庫sql_demo中創(chuàng)建一個名為sqldemo的表。

Python之ORM框架SQLAlchemy是什么

3、向表中添加數(shù)據(jù)。

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Base = declarative_base()  # 導(dǎo)出官方基礎(chǔ)類
class SQLDemo(Base):
    __tablename__ = 'SqlDemo'  # 表名
    # 創(chuàng)建字段
    id = Column(Integer,primary_key=True,autoincrement=True)
    name = Column(String(32))
# 創(chuàng)建數(shù)據(jù)庫引擎
engine = create_engine('mysql+pymysql://root:123@127.0.0.1:3306/sql_demo?charset=utf8')
# 創(chuàng)建表
# Base.metadata.create_all(engine)
# 向表中添加數(shù)據(jù)
obj = SQLDemo(name='小明')
# 創(chuàng)建會話
obj_session = sessionmaker(engine)
# 打開會話
db_session = obj_session()
# 向表中添加數(shù)據(jù),此時數(shù)據(jù)保存在內(nèi)存中
db_session.add(obj)
# 提交數(shù)據(jù),將數(shù)據(jù)保存到數(shù)據(jù)庫中
db_session.commit()
# 關(guān)閉會話
db_session.close()

3.1向表中添加多條數(shù)據(jù)。

 向表中添加多條數(shù)據(jù)

 添加后的數(shù)據(jù)如下:

Python之ORM框架SQLAlchemy是什么

4、查詢表中數(shù)據(jù)

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Base = declarative_base()  # 導(dǎo)出官方基礎(chǔ)類
class SQLDemo(Base):
    __tablename__ = 'SqlDemo'  # 表名
    # 創(chuàng)建字段
    id = Column(Integer,primary_key=True,autoincrement=True)
    name = Column(String(32))
# # 創(chuàng)建數(shù)據(jù)庫引擎
engine = create_engine('mysql+pymysql://root:123@127.0.0.1:3306/sql_demo?charset=utf8')
# 創(chuàng)建會話
obj_session = sessionmaker(engine)
# 打開會話
db_session = obj_session()
# 查詢表中所有數(shù)據(jù)
all_list = db_session.query(SQLDemo).all()
for obj in all_list:
    print(obj.id,obj.name)
# 提交數(shù)據(jù),將數(shù)據(jù)保存到數(shù)據(jù)庫中
db_session.commit()
# 關(guān)閉會話
db_session.close()

 最終打印結(jié)果如下:

Python之ORM框架SQLAlchemy是什么

4.1使用filter過濾查詢條件

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Base = declarative_base()  # 導(dǎo)出官方基礎(chǔ)類
class SQLDemo(Base):
    __tablename__ = 'SqlDemo'  # 表名
    # 創(chuàng)建字段
    id = Column(Integer,primary_key=True,autoincrement=True)
    name = Column(String(32))
# # 創(chuàng)建數(shù)據(jù)庫引擎
engine = create_engine('mysql+pymysql://root:123@127.0.0.1:3306/sql_demo?charset=utf8')
# 創(chuàng)建會話
obj_session = sessionmaker(engine)
# 打開會話
db_session = obj_session()
# 使用filter過濾查詢條件
all_list = db_session.query(SQLDemo).filter(SQLDemo.name=='小明')
for obj in all_list:
    print(obj.id,obj.name)
# 提交數(shù)據(jù),將數(shù)據(jù)保存到數(shù)據(jù)庫中
db_session.commit()
# 關(guān)閉會話

Python之ORM框架SQLAlchemy是什么

5、修改數(shù)據(jù)

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Base = declarative_base()  # 導(dǎo)出官方基礎(chǔ)類
class SQLDemo(Base):
    __tablename__ = 'SqlDemo'  # 表名
    # 創(chuàng)建字段
    id = Column(Integer,primary_key=True,autoincrement=True)
    name = Column(String(32))
# 創(chuàng)建數(shù)據(jù)庫引擎
engine = create_engine('mysql+pymysql://root:123@127.0.0.1:3306/sql_demo?charset=utf8')
# 創(chuàng)建會話
obj_session = sessionmaker(engine)
# 打開會話
db_session = obj_session()
# 更改id是2的name為娃哈哈
all_list = db_session.query(SQLDemo).filter(SQLDemo.id==2).update({'name':'娃哈哈'})
# 提交數(shù)據(jù),將數(shù)據(jù)保存到數(shù)據(jù)庫中
db_session.commit()
# 關(guān)閉會話
db_session.close()

6、刪除數(shù)據(jù)

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Base = declarative_base()  # 導(dǎo)出官方基礎(chǔ)類
class SQLDemo(Base):
    __tablename__ = 'SqlDemo'  # 表名
    # 創(chuàng)建字段
    id = Column(Integer,primary_key=True,autoincrement=True)
    name = Column(String(32))
# # 創(chuàng)建數(shù)據(jù)庫引擎
engine = create_engine('mysql+pymysql://root:123@127.0.0.1:3306/sql_demo?charset=utf8')
# 創(chuàng)建會話
obj_session = sessionmaker(engine)
# 打開會話
db_session = obj_session()
# 刪除id是2的行
all_list = db_session.query(SQLDemo).filter(SQLDemo.id==2).delete()
# 提交數(shù)據(jù),將數(shù)據(jù)保存到數(shù)據(jù)庫中
db_session.commit()
# 關(guān)閉會話
db_session.close()

以上是Python之ORM框架SQLAlchemy是什么的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

網(wǎng)頁名稱:Python之ORM框架SQLAlchemy是什么-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://muchs.cn/article44/deiehe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作品牌網(wǎng)站制作、搜索引擎優(yōu)化、網(wǎng)站制作、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作