美文网首页Python 学习
sqlalchemy更新和插入操作

sqlalchemy更新和插入操作

作者: bianruifeng | 来源:发表于2019-04-26 12:30 被阅读0次
      def save_app_info(self):
            try:
                # update app_info
                print(self.dicts)
                data = db_session.query(App_Info).filter_by(app_id=self.app_id,mall_name=self.mall_name).first()
                if data:   
                    {setattr(data, k, v) for k,v in dicts.items()}
                    print(data)
                else:
                    # insert app_info
                    db_session.execute(App_Info.__table__.insert(), self.dicts)
                db_session.commit()
            except:
                db_session.rollback()
                other.error("save app_data is error,here are details:{}".format(traceback.format_exc()))
            finally:
                db_session.close()

    相关文章

      网友评论

        本文标题:sqlalchemy更新和插入操作

        本文链接:https://www.haomeiwen.com/subject/pwefnqtx.html