当前,要连接到各种数据库,我使用以下命令:
# MySQL
try:
conn = pymssql.connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.database)
except Exception as e:
raise ConnectionError("MsSQL Error: %s" % e)
# Oracle
try:
dsn_tns = cx_Oracle.makedsn(self.host, self.port, self.database)
conn = cx_Oracle.connect(self.user, self.password, dsn_tns)
except Exception as e:
raise ConnectionError("Oracle Error: %s" % e)
# PostgreSQL
try:
conn = psycopg2.connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.database)
except Exception as e:
raise ConnectionError("PostgreSQL Error: %s" % e)
# MsSQL
try:
conn = pymssql.connect(host=self.host, port=self.port, user=self.user, password=self.password, database=self.database)
except Exception as e:
raise ConnectionError("MsSQL Error: %s" % e)
python中是否有一种方法可以使用相同的connector
连接到所有人?例如,类似:
odbc.connect(host, port, user, password, database, engine="MYSQL")