제가 작성한 Python 프로그램에서 다음 코드에 문제가 있습니다. 이 프로그램은 데이터베이스에서 데이터를 가져 와서 입력 한 참조 번호 목록 'GrantRefNumber'
의 목록을 기반으로 Excel 스프레드 시트에 넣습니다.모든 행에 Python/SQL-WHERE 절이 적용되지 않습니다.
작동하지만 어떤 이유로 든 첫 번째 참조 번호에만 'a.reporting_period_id like 'none-'
이 적용되어 있지만 나머지는 적용되지 않습니다.
SQL 코드에서 변수 대체를 사용하여 참조 번호 목록을 문자열에 넣습니다.
도움이 될 것입니다.
SQL :
"SELECT a.fa_reference as [GrantRefNumber],
a.fa_name as [Award Title],
a.location as [RO],
ISNULL(cat.grant_department_name, '') as [Department],
a.funding_start_date as [Start Date],
a.funding_end_date as [End Date],
a.[pi] as [PI ID],
a.pi_initials as [PI Name],
a.pi_surname as [PI Surname],
r1_2 as [Type],
DATEADD(s, cast(last_submitted_date as int), '1970-01-01 00:00:00') as [Submitted Date]
from keywordagreements a inner join entries_publications p on a.id =
p.agreement_id left outer join mrc_categories cat on a.origid = cat.id and cat.centre not in ('2')
where a.[pi] NOT LIKE 'S%' and response_code not like 'Test' and Closed is null
and a.reporting_period_id like 'none-'
and a.funding_organisation not like '%UKSA%' and {}'
Order by [RO], [PI ID], [GrantRefNumber]".format(finalList)
finalList (변수 치환) :
finalList라는 내가 파이썬에서 사용자로부터 얻을 참조 번호의 목록입니다 '항목'
items = dfCall['GrantRefNumber'].values.tolist()
refList = " OR ".join(["a.fa_reference LIKE '%s'" % num for num in items])
finalList = refList[:-1]
데이터를 추출하는 데 PYODBC를 사용하고 있습니다.
이 (필자는 인용 부호를 제거 (이전 코드에서 열)의 일부는 쉽게 읽을 수 있도록)처럼 내 코드의 SQL 보인다 : 나는 보이지 않아요
stringQ = "SELECT a.fa_reference as [GrantRefNumber], a.fa_name as [Award Title]," \
" a.location as [RO], ISNULL(cat.grant_department_name, '') as [Department]," \
" a.funding_start_date as [Start Date], a.funding_end_date as [End Date]," \
" a.[pi] as [PI ID], a.pi_initials as [PI Name]," \
" a.pi_surname as [PI Surname], " \
" r1_2 as [Type], ISNULL(r1_2_1, '') as [PubMed ID]," \
" r1_2_2 as [Author], r1_2_3 as [Publication], ISNULL(r1_2_4, '') as [Journal]," \
" ISNULL(r1_2_8, '') as [Month], ISNULL(r1_2_9, '') as [Year], ISNULL(r1_2_4_1, '') as [Conference]," \
" ISNULL(r1_2_36, '') as [PubMed Central ID], ISNULL (r1_2_19, '') as [DOI]," \
" case when nullif(r1_2_1,'') is not null then 'http://europepmc.org/abstract/MED/' + r1_2_1 else case when" \
" nullif(r1_4,'') is not null then r1_4 else case when nullif(r1_2_19,'') is not null then" \
" 'http://dx.doi.org/' + r1_2_19 else isnull(r1_2_1,'') end end end as [URL], ISNULL(r1_2_21, '') " \
"as [ISBN]," \
" ISNULL(r1_2_30, '') as [ISBN (Electronic)], " \
" ISNULL(r1_2_25, '') as [Chapter Number], " \
"ISNULL(r1_2_26, '')" \
" as [Chapter Title], ISNULL(r1_2_27, '') as [Chapter Author]," \
" ISNULL(r1_2_29, '') as [ISSN (Print)], ISNULL(r1_2_32, '') as [ISSN (Digital)], " \
"ISNULL(r1_2_31, '') as [Web of Science ID], ISNULL(r1_2_34, '') as [Scopus ID], " \
"ISNULL(r1_2_35, '') as [arXiv DepositID]," \
" ISNULL(r1_2_38, '') as [Bibcode], ISNULL(r1_2_39, '') as [Ethos], ISNULL(r1_2_43, '') as [NASA-ADS ID]," \
" ISNULL(r1_2_46, '') as [Inspire], ISNULL(r1_2_40, '') as [PMC Manuscript ID], ISNULL(r1_2_45, '')" \
" as [ORCID Work Putcode]," \
" ISNULL(r1_2_61, '') as [OpenAire Access License], ISNULL(r1_2_52, '') " \
"as [In EPMC?], ISNULL(r1_2_53, '') as [In PMC?]," \
" ISNULL(r1_2_51, '') as [EPMC Open Access], " \
" DATEADD(s, cast(last_submitted_date as int), '1970-01-01 00:00:00') as [Submitted Date] " \
"from keywordagreements a inner join entries_publications p on a.id = " \
"p.agreement_id left outer join mrc_categories cat " \
"on a.origid = cat.id and cat.centre not in ('2') where a.[pi] NOT LIKE 'S%' and " \
"response_code not like 'Test' and Closed is null " \
"and a.reporting_period_id like 'none-' " \
"and a.funding_organisation not like '%UKSA%' and {}' " \
"Order by [RO], [PI ID], [GrantRefNumber]".format(finalList)
안녕하세요, 죄송합니다. 한 줄의 코드 (가장 중요한 비트)를 놓쳤습니다. 원래의 질문에 추가했습니다. 죄송합니다! – ScoutEU
지금 트리플 쿼트 포맷을 확인해 보겠습니다! :) – ScoutEU
불행히도 트리플 쿼트가 작동하지 않습니다. 구문 오류가 발생했습니다 (이미 여러 행을 실행하기 위해 각 행에 따옴표를 넣었습니다.) – ScoutEU