我正在尝试运行SQL代码以在python函数中从IBM DB2检索数据,该函数正在从SAP GUI检索数据,并基于某些条件从IBM DB2提取数据。当我打印DB2的连接时,它可以工作。但是,SQL代码被打印为字符串。请注意,我没有提到要登录到SAP的全部代码,因为这很长。分别运行同一脚本时,可以很好地检索所需的数据。知道为什么将其视为字符串而不是SQL脚本。
查询结果:
<ibm_db_dbi.Connection object at 0x000002B8DF807588>
Select * from DBA.M82 T82
WHERE T82.EID IN 324809
代码是:
import win32com.client
import sys
import subprocess
import time
import pandas as pd
import numpy as np
from datetime import date
from datetime import datetime, timedelta
from multiprocessing import Process
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from time import sleep
from selenium.webdriver.support.ui import Select
import ibm_db
import ibm_db_dbi as db
def sap_login()
dsn = "DRIVER={{IBM DB2 ODBC DRIVER}};" + \
"DATABASE=;" + \
"HOSTNAME=;" + \
"PORT=;" + \
"UID=;" + \
"PWD=;"
hdbc = db.connect(dsn, "", "")
e_id=session.findById("wnd[0]/usr/cntlBCALV_GRID_DEMO_0100_CONT1/shellcont/shell").GetCellValue(i,"ZEMP_CODE")
sql=(""" Select *
from DBA.M82 T82
WHERE T82.EID in {}""").format(e_id)
print(sql)
fsdd=pd.read_sql(sql,hdbc)
sap_login()
import win32com.client
import sys
import subprocess
import time
import pandas as pd
import numpy as np
from datetime import date
from datetime import datetime, timedelta
from multiprocessing import Process
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException
from time import sleep
from selenium.webdriver.support.ui import Select
import ibm_db
import ibm_db_dbi as db
def sap_login()
dsn = "DRIVER={{IBM DB2 ODBC DRIVER}};" + \
"DATABASE=;" + \
"HOSTNAME=;" + \
"PORT=;" + \
"UID=;" + \
"PWD=;"
hdbc = db.connect(dsn, "", "")
e_id=session.findById("wnd[0]/usr/cntlBCALV_GRID_DEMO_0100_CONT1/shellcont/shell").GetCellValue(i,"ZEMP_CODE")
sql=""" Select *
from DBA.M82 T82
WHERE T82.EID in {}""".format(e_id)
print(sql)
fsdd=pd.read_sql(sql,hdbc)
注意,在使用格式为多个值提取数据时,您需要将列表转换为元组。