Как написать запрос на выборку в python, чтобы выбрать из базы данных, где предложение where читает из фрейма данных, состоящего из данных из CSV-файла - PullRequest
0 голосов
/ 29 апреля 2020

Вот мой код, который считывает CSV-файл во фрейм данных и использует его в качестве фильтра для запроса данных из базы данных

import pandas as pd
import pypyodbc
connection = pypyodbc.connect("Driver={SQL Server};"
                      "Server=DCPWDBS1122;"
                      "Database=CLARITY_RPT_CRYSTAL;"
                        "Trusted_Connection=yes;" )
#loads the csv file into the data frame
df_code = pd.read_excel("Test_codes.xlsx",'Test Triggering Codes')
#replacing space by _ in the column name
df_code.columns = [c.replace(' ', '_') for c in df_code.columns]
cursor=connection.cursor()
#this query should select from the database where the column contains the values in the dataframe
sql = "select * from HSP_ACCT_PX_LIST where FINAL_ICD_PX_ID in " + df_code[['Test_Code']]
#the following step gives error TypeError: bytes or integer address expected instead of DataFrame 
#instance
cursor.execute(sql)
result = cursor.fetchall()
for i in result:
    print(i)
...