gpt4 book ai didi

python - 如何使用 Python 编写查询?

转载 作者:行者123 更新时间:2023-12-01 08:20:33 24 4
gpt4 key购买 nike

我正在尝试将变量传递给查询。我找不到办法做到这一点。

我要求两个输入,并且我正在尝试使用这两个输入进行查询。

c.execute('SELECT DESCRIPTION,PART_REV 
FROM ENG_PART_REVISION_REFERENCE
WHERE PART_NO = \'Part_No\' and Rev_no = \'Rev_no\' and STATE = \'Released\' ')

Part_noRev_no 是输入。

import docx
import cx_Oracle
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx import Document
import os


try:
doc = Document()
Part_No = input("Please enter part_no: ")
Rev_no = input("Please, the Rev_no: ")
dsn_tns = cx_Oracle.makedsn('XXXXXXXXXXXXXXXXX', 'XXX', service_name='XXXXXX') #PLease I remove the correct data from this string connection.
conn = cx_Oracle.connect(user=r'lora1app', password='lora1app', dsn=dsn_tns)
c = conn.cursor()
c.execute('SELECT DESCRIPTION,PART_REV FROM ENG_PART_REVISION_REFERENCE WHERE PART_NO = \'Part_No\' and Rev_no = \'Rev_no\' and STATE = \'Released\' ')


doc.add_picture(os.path.join(os.path.dirname(__file__), 'logo.PNG'))
doc.add_heading('Part Change Notification', 0)
doc.add_paragraph('Part Number: '+ ' '+Part_No)
doc.add_paragraph('Rev_no: '+' '+Rev_no)
doc.save(os.path.join(os.path.dirname(__file__),'testing.docx'))

for result in c:
print (result)

c.close()
conn.close()



input("Your Part_No: " +Part_No+" has been update it on the word document.")

except BaseException:
import sys
print(sys.exc_info()[0])
import traceback
print(traceback.format_exc())
finally:
print("Document was updated it")
input()

最佳答案

您需要使用冒号作为绑定(bind)变量

v_sql:="SELECT DESCRIPTION,PART_REV 
FROM ENG_PART_REVISION_REFERENCE
WHERE PART_NO = :Part_No
AND REV_NO = :Rev_No
AND STATE = 'Released' "
c.execute(v_sql, (Part_No, Rev_No))
  • 元组中的顺序应与变量。按顺序,Part_No 是第一个,Rev_No 是第二个。
  • 名称不必相同,而我这样使用它们是因为代码中的变量名称。

关于python - 如何使用 Python 编写查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54675951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com