摘要:相關(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è)人的一些積累。
以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)
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")
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()
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
摘要:標(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 元素...
摘要:標(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 元素...
摘要:其實(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è)...
閱讀 3159·2021-11-24 10:24
閱讀 2979·2021-11-11 16:54
閱讀 3091·2021-09-22 15:55
閱讀 2045·2019-08-30 15:44
閱讀 1912·2019-08-29 18:41
閱讀 2775·2019-08-29 13:43
閱讀 3065·2019-08-29 12:51
閱讀 1210·2019-08-26 12:19