Python에서 mysql 문을 실행할 때 오류를 해결하는 방법

王林
풀어 주다: 2023-05-29 13:34:06
앞으로
1990명이 탐색했습니다.

1 Python은 mysql 문을 실행할 때 오류를 보고합니다.

우리 모두 알고 있듯이 Python에는 변환 메커니즘이 있습니다. %s 및 %d는 문자열이나 숫자로 변환되며 SQL의 퍼지 쿼리도 %를 사용해야 합니다. 퍼지 쿼리를 수행하는 것이 맞습니다. 쿼리 조건이 여전히 변수라면 어색할 것입니다.

해결책은 실제로 매우 간단합니다. SQL에서 퍼지 쿼리가 필요한 문자열을 추출하여 함께 연결하기만 하면 됩니다.

잘못된 방법

<code>shopId = "zbw_010002"</code><code><br></code><code>'''select * from base_saleflows where shopId='{0}' and card_id is not NULL and standard_cls4 like "%湿巾%" '''.format(shopId)</code>
로그인 후 복사

틀린 것을 발견했습니다. mysql은 그러한 명령문을 실행할 수 없습니다.

Python에서 mysql 문을 실행할 때 오류를 해결하는 방법

<code>args='%湿巾%'</code><code>shopId = "zbw_010002"</code><code>mysql_sentence='''select a.shopId, a.sale_money,a.card_id ,a.standard_cls4 from base_saleflows a join  base_vips b on a.card_id = b.card_id where a.shopId='{0}' and a.card_id is not NULL and a.standard_cls4 like '{1}' '''.format(shopId,args)</code><code>print(mysql_sentence)</code><code><br></code>
로그인 후 복사

결과는

select * from base_saleflows a join  base_vips b on a.card_id = b.card_id where a.shopId='zbw_010002' and a.card_id is not NULL and a.standard_cls4 like '%湿巾%'
로그인 후 복사

二 문자열 그룹화 및 접합

flow_no에 따라 cls3 열을 그룹화하고 문자열을 접합합니다. 접합 기호는 '-'

#  分组拼接result = vipsaleflow_common.pivot_table(values='standard_cls3',index='flow_no',aggfunc=lambda x:x.str.cat(sep='-'))
로그인 후 복사

Python에서 mysql 문을 실행할 때 오류를 해결하는 방법

적용: (연도, 분기, 지점 번호, 물티슈 종류) 그룹별로 분기

4개 타임스탬프에서 파생됨 연 월 분기

saleflow['oper_date']=saleflow['oper_date'].astype(str)  #字符串saleflow['oper_date'] = saleflow.oper_date.apply(lambda x:datetime.strptime(x, '%Y-%m-%d %H:%M:%S'))saleflow["month"] = saleflow.oper_date.map(lambda x: x.month)saleflow["year"] = saleflow.oper_date.map(lambda x: x.year)#https://www.it1352.com/584941.htmllookup = {1: 1, 2: 1,3: 1,4: 2, 5: 2, 6: 2, 7: 3,8: 3,9: 3,10: 4, 11: 4,12: 4}saleflow['Season'] = saleflow['oper_date'].apply(lambda x: lookup[x.month]) saleflow['YearMonth'] = saleflow['oper_date'].map(lambda x: 100*x.year + x.month)
로그인 후 복사

위 내용은 Python에서 mysql 문을 실행할 때 오류를 해결하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:yisu.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!