可以在本机运行 ifconfig,查看eth0中指向的 inet 地址

如果该数据库是postgre,则docker中db_mg 的数据库engine可以写作

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

class DatabaseManagement():
    def __init__(self):
        self.engine_postgre = create_engine('postgresql+psycopg2://odoo:odoo12@10.0.0.5/odoo12',echo=True)#连接odoo postgre数据库
        DBsession_postgre = sessionmaker(bind=self.engine_postgre)
        self.session_postgre = DBsession_postgre()

    def add_obj_postgre(self,obj):
        self.session_postgre.add(obj)
        self.session_postgre.commit()
        return obj

    def query_all_postgre(self,target_class,query_filter):
        result_list = self.session_postgre.query(target_class).filter(query_filter).all()
        return result_list

    def update_by_filter_postgre(self,obj,update_hash,query_filter):
        self.session_postgre.query(obj.__class__).filter(query_filter).update(update_hash)
        self.session_postgre.commit


    def delete_by_filter_postgre(self,obj,query_filter):
        self.session_postgre.query(obj).filter(query_filter).delete()


    def close_postgre(self):
        self.session_postgre.close()

    def execute_sql_postgre(self,sql_str):
        try:
            return self.session_postgre.execute(sql_str)
        except Exception as e:
            self.session.rollback()
            return "error"

    def execute_sql_postgre_w(self,sql_str):
        try:
            self.session_postgre.execute(sql_str)
            self.session_postgre.commit()
            return "ok"
        except Exception as e:
            self.session.rollback()
            return "error"

参见:

"从 Docker 容器内部,如何连接到机器的本地主机?" - 堆栈内存溢出

Logo

更多推荐