-
[파이썬]python sql format string / 파라미터 받아서 쿼리 작성하고 싶을 때언어/Python 2022. 12. 9. 11:20
상황 : 넘겨받은 조건을 통해 원하는 값만 db에서 골라 csv 로 생성하는것이 목표
과제 : 쿼리로 넘겨줄 때 where 조건에 대한 값을 config 에서 불러오고 싶다.
여러 방식의 format string 이 있는데, 그 중 나는 이렇게 하니 해결되었다.
sql = f"{값}"
cursor.exectue(sql.format(값)
코드
sql = f"select * from employee where joindate = {currdate}" print(sql) cursor.execute(sql.format(currdate))
결과
다만, query execute까지는 되었는데
조건에 대한 값이 안넘어와서 애매하다.
해결)
sql = f"select * from emp.employee where Joindate = '{currdate}'" cursor.execute(sql.format(currdate))
조건파라미터로 받는건 따옴표처리를 해줘야했다.
joindate의 J대소문자는 크게 중요하지 않음
결과)
반응형'언어 > Python' 카테고리의 다른 글
[pandas] dataframe 컬럼명 변경에러 (1) 2022.12.12 [pandas]pandas를 사용해서 쿼리에 따른 DB값 추출하여 csv로 만들기 (0) 2022.12.09 [파이썬] 로그레벨, 로그 세팅하기, Logging, Logger (1) 2022.12.08 [python] 2중for문 한줄로 작성하기 (list) (0) 2022.11.30 [Python]간단한 파일 입출력, 로그찍기 (RotatingFileHandler) (0) 2022.11.16