1

I am using Python to extract data from SQL by using ODBC to linking Python to SQL database. when I do the query, I need to use variables in the query to make my query result changeable. For example, my code is:

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)

You can see that I retrieve data from SQL database and save it into a list (Result), then I convert this list to a data frame (d).

But, my problems are:

  1. I need to specify a start date and an end data in myCursor.execute part, something like "CALDAY" BETWEEN Start AND End
  2. Let's say I have 100 offices in my data. Now I just need 3 of them (myOffice). So, I need to put a condition in myCursor.execute part, like myOffice in (1,2,3)

In R, I know how to deal with these two problems. the code is like:

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 ***
                       '))

But I do not know how to do this in Python. How can I do this?

1 Answer 1

2

You can use string formatting operations for this.

First define

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" 
"""

Now you can use

myRow = myCursor.execute(query.format(
    start='20180501'
    end='20180501',
    other_conds=''))

and

myRow = myCursor.execute(query.format(
    start='20180501'
    end='20180501',
    other_conds='AND myOffice IN (1,2,3)'))
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I want. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.