子进程是python的cx_oracle的唯一替代 - 连接到oracledb

时间:2016-03-31 00:28:42

标签: python subprocess cx-oracle

我在python中使用了cx_Oracle一段时间,sqlalchemy也在内部使用它连接到oracle。但是最近,我已经使用了一些我无法安装cx_Oracle的solaris盒,所以我想知道在子进程中使用sqlplus似乎是运行查询的唯一方法。

还有其他选择吗?

import cx_Oracle
import logging

class Oracle(object):
  """
    Connector class to connect to Oracle database and run queries
  """
  def __init__(self, db_config):
    # mode should be 0 if not cx_Oracle.SYSDBA
    self.logger = logging.getLogger(__name__)
    if db_config:
      self.username = db_config["username"]
      self.password = db_config["password"]
      self.host = db_config["host"]
      self.port = db_config["port"]
      self.sid = db_config["sid"]
      self.mode = int(db_config["mode"])
      self._cursor = None
      self._connection = None
      self.connect_string = self.username + '/' + self.password + '@' + self.host + ':' + self.port + '/' + self.sid
      try:
        self._connection = cx_Oracle.connect(self.connect_string, mode=self.mode, threaded=True)
        self._cursor = self._connection.cursor()
        self.idVar = self._cursor.var(cx_Oracle.NUMBER)
      except Exception as e:
        print "Exception while trying to initialize database connection object : ", e
    else:
      raise DbConfigNotFoundException

  def param_query(self, q, param):
    try:
      self._cursor.execute(q, param)
      return self._cursor.fetchall()
    except Exception as e:
      logger.exception(e)
      raise

0 个答案:

没有答案