首页 > 解决方案 > Python 3.7.0 c.execute(v_sql,[Part_no, Rev_no]) 无法识别我的变量

问题描述

我一直在尝试使用 Oracle_CX 库从 Oracle DB 中检索数据。但是,当我对 c.execute(v_sql,['02502-9001-12','SS']) 进行硬编码时,它会返回正确的数据。但是,当我必须将变量传递给函数时 c.execute(v_sql,[Part_No,Rev_no]) 无法识别输出为(无)的值

我试图做的是仅当我对数据进行硬编码时才将值传递给 Part_No 和 Rev_no,这样我才能接收到正确的数据。我什至在 c.execute() 发生之前打印,看看是否打印了 Part_No 和 Rev_no 并且它发生了。实际错误是我没有收到正确的数据(无),我想收到 AAAW07AAFAAF5/PAAq。看起来 def why_(Part_no, Rev_no) 的值没有填充到 c.execute(v_sql,[Part_No,Rev_no])

c.execute(v_sql,[Part_No,Rev_no])

c.execute(v_sql,['02502-9001-12','SS'])

import cx_Oracle
import os




def why__(Part_No, Rev_no):
    dsn_tns = cx_Oracle.makedsn('XXXXX', 'XXXX', service_name='XXXXX')
    conn = cx_Oracle.connect(user=r'XXXX', password='XXXX', dsn=dsn_tns)
    c = conn.cursor()
    #u_sql =('SELECT PART_REV_CFP.GET_OBJKEY(:Part_No,:Rev_no) from dual')
    v_sql =('SELECT PART_REV_CFP.GET_OBJKEY(:Part_No,:Rev_no) from dual')
    print(Part_No)
    print(Rev_no)
    c.execute(v_sql,[Part_No,Rev_no])
    #c.execute(v_sql,['02502-9001-12','SS'])
    R = c.fetchone()
    print('before the loop')
    i=0
    for row in R:
        print('inside the loop')
        i+=1
        print(i)
        print(str(row))
    print('after the loop')
    conn.close()
    return row

Part_No = input("Enter the obs part num: ")
Rev_no = input("Enter the obs part rev: ")
why__(Part_No,Rev_no)
print('outside of the method')
print(why__(Part_No,Rev_no))

OutPut when c.execute(v_sql,[Part_No,Rev_no]) --IN other words no hard coding the values of Part_no and Rev_no



Enter the obsolete part number: 02502-9001-12
Enter the obsolete part revision: ss
before the loop
inside the loop
1
(None,)
after the loop

***Repl Closed***




Output when c.execute(v_sql,['02502-9001-12','SS'])



Enter the obsolete part number: 02502-9001-12
Enter the obsolete part revision: ss
02502-9001-12
ss
before the loop
inside the loop
1
AAAW07AAFAAF5/PAAq
after the loop
outside of the method
02502-9001-12
ss
before the loop
inside the loop
1
AAAW07AAFAAF5/PAAq
after the loop
AAAW07AAFAAF5/PAAq

***Repl Closed***

标签: pythonpython-3.x

解决方案


推荐阅读