Mysql CREATE TABLE IF NOT EXISTS dynamic table name - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Mysql CREATE TABLE IF NOT EXISTS dynamic table name (/thread-26375.html) |
Mysql CREATE TABLE IF NOT EXISTS dynamic table name - nisusavi - Apr-29-2020 Hi folks, need some help here. I am trying to create a table in mysql from python and I get a syntax error while my print command shows the correct syntax. Any help is much appreciated. symbol="TSLA" option_date_head="2020-04-24" create_table_sql = ("""CREATE TABLE IF NOT EXISTS """ + symbol + "-" + option_date_head + """ (option_expiry_date date, call_ask float, strikeprice int, put_ask float);""") print(create_table_sql) mycursor.execute(create_table_sql)This is the print command output CREATE TABLE IF NOT EXISTS TSLA-2020-05-01 (option_expiry_date date, call_ask float, strikeprice int, put_ask float);And this is the error Figured it. Minor change was requiredIt required backticks for the tablename create_table_sql = ("""CREATE TABLE IF NOT EXISTS `""" + symbol + "-" + option_date_head + """` (option_expiry_date date, call_ask float, strikeprice int, put_ask float);""")For some reason Backticks are not displayed in the above command. I had a backtick before """ + symbol and one after option_date_head + """ |