mysql query from backend python server syntax error with backticks on table name

11 Views Asked by At

Python 3.9 server with flask + mysql 8.0.24

When I run the query as

sqlQuery = """INSERT INTO %s (baseball_card_id, brand, buy_date, buy_price, card_condition, card_number,
        description, first_name, last_name, profit_loss, sell_date, sell_price, year, card_image_front, card_image_back, front_public_id, back_public_id) 
        VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""" 

        bind_data = (account_number, baseball_card_id, brand, buy_date, buy_price, card_condition, card_number, description, first_name, last_name, profit_loss, sell_date, sell_price, year, image_front_url, image_back_url, front_public_id, back_public_id)

        cur.execute(sqlQuery, bind_data)

I get the error

Error: '1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''jvtojosbauxofhevhfee' (this is the table name) (baseball_card_id, brand, buy_date, buy_price, card_condi' at line 1'

My experience so far tells me I just need backticks for the table name, like so:

sqlQuery = """INSERT INTO `%s` (baseball_card_id, brand, buy_date, buy_price, card_condition, card_number,
description, first_name, last_name, profit_loss, sell_date, sell_price, year, card_image_front, card_image_back, front_public_id, back_public_id) 
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""" 

but when I run that, it fails, and in the server log, the query shows as (note the backtick then single quote)

INSERT INTO `'jvtojosbauxofhevhfee'` (baseball_card_id, brand, buy_date, buy_price, card_condition, card_number, description, first_name, last_name, profit_loss, sell_date, sell_price, year, card_image_front, card_image_back, front_public_id, back_public_id) VALUES('0e993307-231c-44f0-908d-b38786199851', '2222', '2021-04-28T04:00:00.000Z', '2222', '2222', '2222', '2222', '2222', '2222', '', '', '', '2222', '', '', '', '')

I have seen this error before and just adding the backticks to the table name in the sql query always solved it, but not this time. I even renamed the table name as all letters in case it was a syntax issue with dashes, underscores, etc.

Can anyone tell me what I am missing?

0

There are 0 best solutions below