成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

sqlaclhemy 相關(guān)用法

韓冰 / 881人閱讀

摘要:相關(guān)用法最大的好處在于結(jié)構(gòu)清晰化,以及遷移數(shù)據(jù)庫(kù)不會(huì)造成過(guò)多的冗余。但是缺點(diǎn)是沒(méi)有純粹的的功能多,也沒(méi)有純粹的來(lái)的方便。但對(duì)于開(kāi)發(fā)人員來(lái)講,最大的好處在于閱讀代碼更加直觀。

sqlalchemy 相關(guān)用法

sqlalchemy 最大的好處在于結(jié)構(gòu)清晰化,以及遷移數(shù)據(jù)庫(kù)不會(huì)造成過(guò)多的冗余。但是缺點(diǎn)是沒(méi)有純粹的的sql功能多,也沒(méi)有純粹的sql來(lái)的方便。但對(duì)于開(kāi)發(fā)人員來(lái)講,sqlalchmey最大的好處在于閱讀代碼更加直觀。

本文主要我多年來(lái)使用sqlalchemy遇到的一些坑,以及應(yīng)該如何去做,以及剛開(kāi)始萌新容易遇到的錯(cuò)誤,希望我這篇文章能夠給予我個(gè)人的一些積累。

如何構(gòu)建一個(gè)鏈接

MYSQL為例,注意password中不要包含@

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db_url = "mysql+pymysql://username:password@localhost:port/datebase"
app.config["SQLALCHEMY_DATABASE_URI"] = db_url
app.config["SQLALCHEMY_COMMIT_ON_TEARDOWN"] = True # 自動(dòng)提交
app.config["SQLALCHEMY_ECHO"] = False  # 如果True在打印出sql日志
db = SQLAlchemy(app)
如何創(chuàng)建一個(gè)數(shù)據(jù)表并可以支持繼承重構(gòu)
class BaseRoom(db.Model):
    __abstract__ = True  # 生命跳過(guò)該class表的生成
    id = db.Column(db.String(50), primary_key=True, comment="用戶(hù)的id")
    third_id = db.Column(db.String(50), nullable=False, comment="網(wǎng)站的id")
    title = db.Column(db.String(500), nullable=False, comment="題目")
    price = db.Column(db.Float(asdecimal=True), nullable=False, comment="價(jià)格")
    
   
   
class DaxiangRoom(BaseRoom):
    __tablename__ = "daxiang_daxiangroom" # 表名字
    __table_args__ = {"useexisting": True}

    landlord_id = db.Column(db.ForeignKey("daxiang_daxianglandlord.id"), index=True)
    landlord = db.relationship("DaxiangLandlord",
                               primaryjoin="DaxiangRoom.landlord_id == DaxiangLandlord.id",
                               backref="daxiang_daxiangrooms")
進(jìn)行一個(gè)session的提交,查找,更新
class CtripRoomSqlAlchemyPipeline(object):

    # 將每行更新或?qū)懭霐?shù)據(jù)庫(kù)中
    def process_item(self, item, spider):
    

        model = CtripRoom(hotel_id=item["hotel_id"],
                          hotel_name=item["hotel_name"],
                          city=item["city"],
                          location=item["location"],
                          type=item["type"],
                          score=item["score"]
                          )
        try:
            db.session.add(model)
            db.session.commit()
        except Exception as e:
            # print(e)
            db.session.rollback()
            pass
        return item
        
        
class lukeRoomUpdateSqlAlchemyPipeline(object):

    # 更新sql
    def process_item(self, item, spider):
        landlord_id = item["landlord_id"]
        model = LukeRoom.query.filter_by(id=item["house_id"]).first()

        model.landlord_id = landlord_id
        model.is_finished = 1

        db.session.add(model)
        db.session.commit()
        
   
使用sqlalchemy 實(shí)現(xiàn)ON DUPLICATE KEY UPDATE
from dbs.database import db
from sqlalchemy.dialects.mysql import insert
token = content["token"]
city = content["city"]
account = content["account"]
platform = "airbnb"
user_id = content["user_id"]
_id = platform + "_" + content["user_id"]
_time = datetime.now(timezone("Asia/Shanghai")).strftime("%Y-%m-%d %H:%M:%S")

# 插入的數(shù)據(jù)
comment_model = insert(User).values(
    id=_id, platform=platform, city=city, token=token, user_id=user_id, create_time=_time, account=account
)

# 出現(xiàn)沖突后需要更新的數(shù)據(jù)
do_update_comment_model = comment_model.on_duplicate_key_update(
    id=_id, platform=platform, city=city, token=token, user_id=user_id, create_time=_time, account=account,
    user_status=""
)
db.session.execute(do_update_comment_model)
db.session.commit()

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/45278.html

相關(guān)文章

  • 補(bǔ)習(xí)前端(css+html)基礎(chǔ)-1:

    摘要:標(biāo)簽將表單內(nèi)容的一部分打包,生成一組相關(guān)表單的字段。提示和注釋注釋元素是空元素,它僅包含屬性。注釋此元素只能存在于部分,不過(guò)它可出現(xiàn)任何次數(shù)。屬性屬性規(guī)定當(dāng)前文檔與被鏈接文檔之間的關(guān)系。該標(biāo)簽用于組合表格的主體內(nèi)容。 1.HTML 標(biāo)簽: 實(shí)例 組合表單中的相關(guān)元素: health information height: weight: 定義和用法 fieldset 元素...

    marek 評(píng)論0 收藏0
  • 補(bǔ)習(xí)前端(css+html)基礎(chǔ)-1:

    摘要:標(biāo)簽將表單內(nèi)容的一部分打包,生成一組相關(guān)表單的字段。提示和注釋注釋元素是空元素,它僅包含屬性。注釋此元素只能存在于部分,不過(guò)它可出現(xiàn)任何次數(shù)。屬性屬性規(guī)定當(dāng)前文檔與被鏈接文檔之間的關(guān)系。該標(biāo)簽用于組合表格的主體內(nèi)容。 1.HTML 標(biāo)簽: 實(shí)例 組合表單中的相關(guān)元素: health information height: weight: 定義和用法 fieldset 元素...

    douzifly 評(píng)論0 收藏0
  • 關(guān)于javascript中的bind、call、apply等函數(shù)的用法

    摘要:其實(shí)它們都很簡(jiǎn)單,但是在處理一些與相關(guān)的函數(shù)的時(shí)候,用來(lái)改變函數(shù)中的指向,卻是必不可少的工具,所以必須掌握好它們的用法。 關(guān)于javascript中的bind、call、apply等函數(shù)的用法 我GitHub上的菜鳥(niǎo)倉(cāng)庫(kù)地址: 點(diǎn)擊跳轉(zhuǎn)查看其他相關(guān)文章 文章在我的博客上的地址: 點(diǎn)擊跳轉(zhuǎn) ? ? ? ? 前面的文章已經(jīng)說(shuō)到this的指向了,那么這篇文章就要說(shuō)一說(shuō)和this相關(guān)的三個(gè)...

    lordharrd 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<