【问题标题】:Calling Class, getting TypeError: unbound method must be called [duplicate]调用类,得到类型错误:必须调用未绑定的方法[重复]
【发布时间】:2016-12-06 21:58:09
【问题描述】:

我已经查看了 Stackoverflow 上的错误,但我看到的解决方案都没有解决我的问题。我正在尝试为 cx_Oracle 创建一个类,以将我的数据库连接放在一个类中,并在我的数据库实例期间调用它。 我在 C# 中创建了类似的类,但由于某种原因,python 尤其困难。任何帮助表示赞赏。

我利用了这里找到的代码: cx_Oracle and Exception Handling - Good practices?

    import sys
    import os
    import cx_Oracle

    class Oracle(object):

        __db_server = os.getenv("ORACLE_SERVER")
        __db_user = os.getenv("ORACLE_ACCT")
        __db_password = os.getenv("ORACLE_PWD")

        def connect(self):
            """ Connect to the database. """

            try:
                self.db = cx_Oracle.connect(__db_user+'/'+__db_password+'@'+__db_server)
            except cx_Oracle.DatabaseError as e:
                error, = e.args
                if error.code == 1017:
                    print('Please check your credentials.')
                else:
                    print('Database connection error: %s'.format(e))
                # Very important part!
                raise

            # If the database connection succeeded create the cursor
            # we-re going to use.
            self.cursor = db.Cursor()

        def disconnect(self):
            """
            Disconnect from the database. If this fails, for instance
            if the connection instance doesn't exist we don't really care.
            """

            try:
                self.cursor.close()
                self.db.close()
            except cx_Oracle.DatabaseError:
                pass

        def execute(self, sql, bindvars=None, commit=False):
            """
            Execute whatever SQL statements are passed to the method;
            commit if specified. Do not specify fetchall() in here as
            the SQL statement may not be a select.
            bindvars is a dictionary of variables you pass to execute.
            """

            try:
                self.cursor.execute(sql, bindvars)
            except cx_Oracle.DatabaseError as e:
                error, = e.args
                if error.code == 955:
                    print('Table already exists')
                elif error.code == 1031:
                    print("Insufficient privileges")
                print(error.code)
                print(error.message)
                print(error.context)

                # Raise the exception.
                raise

            # Only commit if it-s necessary.
            if commit:
                self.db.commit()

        def select(self, sql, commit=False):        
            bindvars=None
            result = None
            try:
                self.cursor.execute(sql, bindvars)
                result = self.cursor.fetchall()
            except cx_Oracle.DatabaseError as e:
                error, = e.args
                print "Database Error: failed with error code:%d - %s" % (error.code, error.message)
                raise
            if commit:
                self.db.commit()
            return result

        def commit(self):
            try:
                self.db.commit()
            except cx_Oracle.DatabaseError as e:
                error, = e.args
                print "Database Commit failed with error code:%d - %s" % (error.code, error.message)
                raise

        def rollback(self):
            try:
                self.db.rollback()
            except cx_Oracle.DatabaseError as e:
                error, = e.args
                print "Database Rollback failed with error code:%d - %s" %(error.code, error.message)
                raise

这是我的调用例程

    import sys
    import os
    #import cx_Oracle
    from Oracle import Oracle

    def main():
        oracle = Oracle.connect()
        query = """SELECT DISTINCT NAME FROM MyTable"""
        data = oracle.select(query)
        for row in data:
            print row
        oracle.disconnect()

    ### MAIN
    if __name__ == '__main__':
        main()

关于相关说明:我似乎无法让 Python 找到我的 Oracle.py 类,除非它与调用函数位于同一目录中。

艾伦

【问题讨论】:

  • 你要实例化这个类吗?
  • (不是这样)专业提示:我通过谷歌搜索与错误相关的问题标题部分,在 StackOverflow 上找到了链接的副本。
  • 是的,我的问题和标记的重复项中引用的示例似乎都不完整。因此导致了其他问题。

标签: python cx-oracle


【解决方案1】:

您必须创建类的实例才能使用它:

orcl = Oracle()
orcl.connect()
...

【讨论】:

    猜你喜欢
    • 2016-09-15
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2019-09-19
    • 2016-10-14
    • 2017-09-03
    • 1970-01-01
    相关资源
    最近更新 更多