【问题标题】:How do you bind variables in cx_Oracle with Python3.x?如何将 cx_Oracle 中的变量与 Python3.x 绑定?
【发布时间】:2017-08-15 09:59:04
【问题描述】:

我希望你能帮助我,因为我已经被这个问题困扰了一段时间。第一次在这里发帖,什么不是。我先说我是一名自动化测试人员,有一点 Python 经验,但我不是专家。

我一直在绞尽脑汁试图解决这个问题,我不知道这是 cx_Oracle 的限制还是什么,但我找不到有类似问题的人。我几乎没有使用 cx_Oracle 的经验。

所以,这是我正在尝试做的一个小例子。我无法发布整个内容,因为我对其进行了多少更改和移动,这有点混乱。我只是想尝试做一个我所拥有的非常基本的版本;

    def test(self):
        #Driver is set up
        var1 = driver.find_element_by_name('blah').text
        var1 = str(var1)
        #Do some other stuff on the page
        return var1

    def foo():
        #Setup cx_Oracle and connect to DB
        sql = ('SELECT value FROM table WHERE ref = :1')
        c.execute(sql, (var1,)
        #Verify the results of the query

    class testcase(unittest.Testcase):
        def test_case(self):
            setUp(self)
            test(self)
            foo()

没有错误消息或类似的东西。它似乎没有看到变量的值。当我硬编码 var1 的值时,它可以工作并返回结果,就好像我通过 DBeaver 查询了数据库一样。

我尝试将所有内容放在同一个函数中并获得相同的结果。

我尝试如下传递 var1,但仍然得到相同的结果:

    def test(self):
        #Driver is set up
        var1 = driver.find_element_by_name('blah').text
        #Do some other stuff on the page
        return var1

    def foo(var1):
        #Setup cx_Oracle and connect to DB
        sql = ('SELECT value FROM table WHERE ref = :1')
        c.execute(sql, (var1,))
        #Verify the results of the query

    class testcase(unittest.Testcase):
        def test_case(self):
            setUp(self)
            var1 = test(self)
            print (var1)
            foo(var1)

我一直在使用this as a reference for binding variables,但这些方法都不适合我。

所以,总而言之,我有 2 个函数,我想将一个变量从一个传递到另一个,但 cx_Oracle 似乎不喜欢它。我选择 cx_Oracle 是因为 pyodbc 和 pypyodbc 似乎不想连接到我正在尝试与之交谈的 Oracle 10g DB。如果有人知道更好的工具,我什至愿意使用另一种工具?

如果您需要更多详细信息,请告诉我。

【问题讨论】:

    标签: python sql oracle selenium cx-oracle


    【解决方案1】:

    当遇到这样的问题时,在使用变量之前插入一些打印变量会很有帮助,这样您就知道您实际上使用的是正确的。对您的代码做一些更改:

    def test(self):
        #Driver is set up
        var1 = str(driver.find_element_by_name('blah').text)
        #var1 is a local variable to function test
    
        #Do some other stuff on the page
        return var1
    
    def foo(var1):
        #Setup cx_Oracle and connect to DB
        sql = ('SELECT value FROM table WHERE ref = :1')
        c.execute(sql, (var1,))
        #Verify the results of the query
    
    class testcase(unittest.Testcase):
        def test_case(self):
            setUp(self)
            var1 = test(self)
            print (var1)
            foo(var1)
    

    【讨论】:

    • 谢谢。这不是完整的代码,它只是为了让我了解我正在尝试做的事情。里面有很多打印语句哈哈。感谢您整理我的代码。就像我说的,我不是专家,所以这会有所帮助!
    【解决方案2】:

    你可以使用string.format来构建sql

    def foo(var1):
        #Setup cx_Oracle and connect to DB
        sql = 'SELECT value FROM table WHERE ref = {}'.format(var1)
        c.execute(sql)
    

    【讨论】:

    • 认为这肯定有帮助。现在的问题是,当 var1 到达 foo() 时,它变成了 None 类型。所以,其他地方出了问题。关于为什么会发生这种情况的任何建议?
    • 您需要在调用 foo(var1) 之前将调用 test 的返回值分配给 var1,如 Stefan 的回答中所示。 var1 = test(self)
    • 已排序。我不知何故删除了退货。我不知道何时或如何,但现在它正在工作。非常感谢。
    • @AutomaticFish 很高兴为您提供帮助!
    • 请使用绑定变量!将变量直接格式化到 SQL 语句中会导致 SQL 注入和性能降低等问题。
    猜你喜欢
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 2021-06-16
    • 2011-11-02
    • 2018-09-24
    • 2018-05-16
    • 2011-05-19
    相关资源
    最近更新 更多