我使用Python通过使用ODBC将Python链接到SQL数据库来从SQL中提取数据。当我进行查询时,我需要在查询中使用变量来使我的查询结果可更改。例如,我的代码是:
import pyodbc
myConnect = pyodbc.connect('DSN=B1P HANA;UID=***;PWD=***')
myCursor = myConnect.cursor()
Start = 20180501
End = 20180501
myOffice = pd.Series([1,2,3])
myRow = myCursor.execute("""
SELECT "CALDAY" AS "Date",
"/BIC/ZSALE_OFF" AS "Office"
FROM "SAPB1P"."/BIC/AZ_RT_A212"
WHERE "CALDAY" BETWEEN 20180501 AND 20180501
GROUP BY "CALDAY","/BIC/ZSALE_OFF"
""")
Result = myRow.fetchall()
d = pd.DataFrame(columns=['Date','Office'])
for i in Result:
d= d.append({'Date': i.Date,
'Office': i.Office},
ignore_index=True)
您可以看到我从SQL数据库中检索数据并将其保存到列表中(结果),然后我将此列表转换为数据框(d)。
但是,我的问题是:
"CALDAY" BETWEEN Start AND End
myOffice in (1,2,3)
在R中,我知道如何处理这两个问题。代码就像:
office_clause = ""
if (myOffice != 0) {
office_clause = paste(
'AND "/BIC/ZSALE_OFF" IN (',paste(myOffice, collapse=", "),')'
)
}
a <- sqlQuery(ch,paste(' SELECT ***
FROM ***
WHERE "CALDAY" BETWEEN',Start,'AND',End,'
',office_clause1,'
GROUP BY ***
'))
但我不知道如何在Python中这样做。 有人可以帮帮我吗?
答案 0 :(得分:1)
您可以使用string formatting operations。
首先定义
query = """
SELECT
"CALDAY" AS "Date",
"/BIC/ZSALE_OFF" AS "Office"
FROM
"SAPB1P"."/BIC/AZ_RT_A212"
WHERE
"CALDAY" BETWEEN {start} AND {end}
{other_conds}
GROUP BY
"CALDAY","/BIC/ZSALE_OFF"
"""
现在你可以使用
myRow = myCursor.execute(query.format(
start='20180501'
end='20180501',
other_conds=''))
和
myRow = myCursor.execute(query.format(
start='20180501'
end='20180501',
other_conds='AND myOffice IN (1,2,3)'))