Env

  • OS : Ubuntu 20.04
  • python : 3.10.13

Issue

  • DB Insert 구문 작동시 오류 발생
conn = pymysql.connect(
    host = config.DATABASE_CONFIG['host'],
    user = config.DATABASE_CONFIG['user'],
    password = config.DATABASE_CONFIG['password'],
    db = config.DATABASE_CONFIG['db']
)
curs = conn.cursor(pymysql.cursors.DictCursor)

sql = """INSERT INTO TEST (id, order, recom_code, recom_score) VALUES (%s, %s, %s, %s)"""

for idx in range(len(df)):
    curs.execute(sql, tuple(df.values[idx]))
    
conn.commit()
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 'order, recom_code, recom_score) VALUES ('xxx', 'xxx', 'xxx', 'xxx')' at line 1")

해결

  • DB의 컬럼명에 예약어를 피해서 작성 or 컬럼명을 ``로 묶어주기
sql = """INSERT INTO TEST (id, `order`, recom_code, recom_score) VALUES (%s, %s, %s, %s)"""

Reference