日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

基于Python ORM框架的應(yīng)用開發(fā)

本文介紹基于Python ORM框架連接和使用寬表引擎的方法。

前提條件

操作步驟

  1. 執(zhí)行以下命令,安裝MySQL數(shù)據(jù)庫連接模塊PyMySQL。

    pip install PyMySQL
  2. 執(zhí)行以下命令,安裝Python的ORM框架SQLAlchemy。

    pip install SQLAlchemy
  3. 配置連接參數(shù)。

    engine = create_engine('mysql+pymysql://<user>:<password>@<lindorm_mysql_addr>:33060/<database>', echo=True)

    參數(shù)說明

    參數(shù)

    說明

    user

    如果您忘記用戶密碼,可以通過Lindorm寬表引擎的集群管理系統(tǒng)修改密碼。具體操作,請參見修改用戶密碼

    password

    lindorm_mysql_addr

    Lindorm寬表引擎的MySQL兼容地址。如何獲取,請參見查看連接地址

    重要
    • 如果應(yīng)用部署在ECS實例,建議您通過專有網(wǎng)絡(luò)訪問Lindorm實例,可獲得更高的安全性和更低的網(wǎng)絡(luò)延遲。

    • 如果應(yīng)用部署在本地,在通過公網(wǎng)連接Lindorm實例前,需在控制臺開通公網(wǎng)地址。開通方式:在控制臺選擇數(shù)據(jù)庫連接 > 寬表引擎,在寬表引擎頁簽單擊開通公網(wǎng)地址

    • 通過專有網(wǎng)絡(luò)訪問Lindorm實例,lindorm_mysql_addr請?zhí)顚慚ySQL兼容地址對應(yīng)的專有網(wǎng)絡(luò)地址。通過公網(wǎng)訪問Lindorm實例,lindorm_mysql_addr請?zhí)顚慚ySQL兼容地址對應(yīng)的公網(wǎng)地址。

    database

    需要連接的數(shù)據(jù)庫名稱。默認連接default數(shù)據(jù)庫。

  4. 連接并使用Lindorm寬表引擎。以創(chuàng)建表并插入數(shù)據(jù)為例。

    #建表
    Base.metadata.create_all(engine)
    
    #創(chuàng)建session對象:
    session = Session()
    #創(chuàng)建Player對象:
    new_player = Player(player_id = 1001, player_name = "john", player_height = 2.08)
    #寫入到Lindorm
    session.add(new_player)
    #寫入多條數(shù)據(jù)到Lindorm
    session.add_all([
        Player(player_id=1002, player_name="bob", player_height=1.65),
        Player(player_id=1003, player_name="smith", player_height=1.82),
    ])
    session.commit()

完整示例

完整示例代碼如下:

from sqlalchemy import create_engine
from sqlalchemy import Column, String, Integer, Float
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker


Base = declarative_base()
# 定義 Player 對象:
class Player(Base):
    # 表的名字:
    __tablename__ = 'player'
    # 表的結(jié)構(gòu):
    player_id = Column(Integer, primary_key=True,autoincrement=False)
    player_name = Column(String(255))
    player_height = Column(Float)
    def __str__(self):
        return " ".join(str(item) for item in (self.player_id,self.player_name,self.player_height))


# 初始化數(shù)據(jù)庫連接,請根據(jù)實際情況修改連接參數(shù)
engine = create_engine('mysql+pymysql://user:test@ld-bp1l39h99192d****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com:33060/default', echo=True)
Session = sessionmaker(bind=engine)

#建表
Base.metadata.create_all(engine)

# 創(chuàng)建session對象:
session = Session()
# 創(chuàng)建Player對象:
new_player = Player(player_id = 1001, player_name = "john", player_height = 2.08)
# 寫入到Lindorm
session.add(new_player)
# 寫入多條數(shù)據(jù)到Lindorm
session.add_all([
    Player(player_id=1002, player_name="bob", player_height=1.65),
    Player(player_id=1003, player_name="smith", player_height=1.82),
])
session.commit()

# 查詢player_id為1001的數(shù)據(jù)
rows = session.query(Player).filter(Player.player_id == 1001).all()
print([row.__str__() for row in rows])


# 刪除player_id為1002的數(shù)據(jù)
session.query(Player).filter(Player.player_id == 1002).delete()
session.commit()

# 更新player_id為1003的數(shù)據(jù)
session.query(Player).filter(Player.player_id == 1003).update({"player_name":"brown"})
session.commit()

# 查詢所有數(shù)據(jù)
rows = session.query(Player).all()
print([row.__str__() for row in rows])

執(zhí)行成功后將返回如下查詢結(jié)果

['1001 john 2.08']
['1001 john 2.08', '1003 brown 1.82']