반응형
Before
hour = datetime.datetime.now().strftime('%Y-%m-%d %H:00:00)
q_sql = f"""
select *
from example
where reg_date={hour};
"""
Error
pymysql.err.ProgrammingError: (1064, "You have an error in your
SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '14:00:00' at line 3")
syntax 오류가 있다는 내용의 에러를 만났다. sql 쿼리에 문법적인 문제가 있는 것이다.
살펴보니, hour가 문자열이지만 쿼리에 fstring 적용 시 변수가 quote(') 없이 인식이 되어 reg_date=2023-08-02 14:00:00 식으로 입력이 되고 있었던 것이다.
쿼리의 fstring 변수에 quote(')를 적용시켜주면 정상적으로 작동한다.
After
hour = datetime.datetime.now().strftime('%Y-%m-%d %H:00:00)
q_sql = f"""
select *
from example
where reg_date='{hour}';
"""
반응형
'Python' 카테고리의 다른 글
[python] datetime columns difference 구하기 (0) | 2023.08.04 |
---|---|
[python] DatetimeIndex 만들기 - pandas.date_range (0) | 2023.08.03 |
[python] DataFrame 결합 - pandas.DataFrame.join (0) | 2023.08.02 |
[python] DataFrame 결합 - pandas.merge (0) | 2023.07.31 |
[python] DataFrame 결합 - pandas.concat (0) | 2023.07.28 |