Commit dcdedf9e authored by 张晓彤's avatar 张晓彤

数据库自动切换

parent c6f854b9
...@@ -21,73 +21,43 @@ json_file = os.path.join(relative_path, "config.json") ...@@ -21,73 +21,43 @@ json_file = os.path.join(relative_path, "config.json")
# json_file = "config.json" # json_file = "config.json"
with open(json_file) as f: with open(json_file) as f:
mysql_config = json.load(f)["mysql"] load_vale = json.load(f)
with open(json_file) as f: postgre_config = load_vale["postgresql"]
postgre_config = json.load(f)["postgresql"]
# logger = get_logger("zxt.tables") if "mysql" in load_vale:
mysql_config = load_vale["mysql"]
else:
mysql_config = None
# # 创建对象的基类: if "mssql" in load_vale:
# Base = declarative_base() sqlserver_config = load_vale["mssql"]
# else:
# engine_mysql = create_engine( sqlserver_config = None
# "mysql+mysqlconnector://root:%s@192.168.28.111:3306/waytous"
# % quote("Huituo@123")
# )
#
# engine_postgre = create_engine(
# "postgresql://postgres:%s@192.168.28.111:5432/shenbao_2021520"
# % quote("Huituo@123")
# )
# # 创建DBsession_mysql类型:
# DBsession_mysql = sessionmaker(bind=engine_mysql)
#
# DBsession_mysql = scoped_session(DBsession_mysql)
#
# DBsession_postgre = sessionmaker(bind=engine_postgre)
#
# DBsession_postgre = scoped_session(DBsession_postgre)
#
# # 创建session_mysql对象:
# session_mysql = DBsession_mysql()
#
# session_mysql.expire_on_commit = False
#
# session_postgre = DBsession_postgre()
#
# session_postgre.expire_on_commit = False
# 创建对象的基类: # 创建对象的基类:
Base = declarative_base() Base = declarative_base()
sql_str = str("mysql+mysqlconnector://" + mysql_config["user"] + ":%s@" + mysql_config["host"] + \ if mysql_config is not None:
sql_str = str("mysql+mysqlconnector://" + mysql_config["user"] + ":%s@" + mysql_config["host"] + \
":" + mysql_config["port"] + "/" + mysql_config["database"]) ":" + mysql_config["port"] + "/" + mysql_config["database"])
postgre_str = str("postgresql://" + postgre_config["user"] + ":%s@" + postgre_config["host"] + \ engine_mysql = create_engine(sql_str % quote(mysql_config["password"]))
":" + postgre_config["port"] + "/" + postgre_config["database"])
# try: else:
engine_mysql = create_engine(
# "mysql+mysqlconnector://root:%s@192.168.28.111:3306/waytous"
# % quote("Huituo@123")
# "mysql+mysqlconnector://" + mysql_config["user"] + ":" + mysql_config["password"] + "@" + mysql_config[ sql_str = str("mssql+pymssql://" + sqlserver_config["user"] + ":%s@" + sqlserver_config["host"] + \
# "host"] + ":" + mysql_config["port"] + "/" + mysql_config["database"] ":" + sqlserver_config["port"] + "/" + sqlserver_config["database"])
sql_str % quote(mysql_config["password"]) engine_mysql = create_engine(sql_str % quote(sqlserver_config["password"]))
)
engine_postgre = create_engine( postgre_str = str("postgresql://" + postgre_config["user"] + ":%s@" + postgre_config["host"] + \
# "postgresql://postgres:%s@192.168.28.111:5432/shenbao_2021520" ":" + postgre_config["port"] + "/" + postgre_config["database"])
# % quote("Huituo@123")
# "postgresql://" + postgre_config["user"] + ":" + postgre_config["password"] + "@" + postgre_config[ # try:
# "host"] + ":" + postgre_config["port"] + "/" + postgre_config["database"]
postgre_str % quote(postgre_config["password"]) engine_postgre = create_engine(postgre_str % quote(postgre_config["password"]))
)
# 创建DBsession_mysql类型: # 创建DBsession_mysql类型:
DBsession_mysql = sessionmaker(bind=engine_mysql) DBsession_mysql = sessionmaker(bind=engine_mysql)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment