Python Forum
Mysql CREATE TABLE IF NOT EXISTS dynamic table name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mysql CREATE TABLE IF NOT EXISTS dynamic table name
#1
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

Error:
_mysql_connector.MySQLInterfaceError: 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 '-2020-05-01 (option_expiry_date date, call_ask float, strikeprice int, put_ask f' at line 1
Figured it. Minor change was required

It 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 + """
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 640 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  Remove Blank Lines from docx table and paragraphs bsudhirk001 1 160 Feb-14-2021, 12:38 AM
Last Post: Larz60+
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 155 Feb-05-2021, 04:37 PM
Last Post: nilamo
  write mariadb table rows query to each file? shams 1 141 Feb-02-2021, 04:10 PM
Last Post: buran
  Convert table in pandas tgottsc1 2 198 Jan-26-2021, 01:58 PM
Last Post: tgottsc1
  How to update values in a pyarrow table? xraphael75 1 249 Jan-25-2021, 02:14 PM
Last Post: xraphael75
  Populate a table with variables Market_Python 4 291 Jan-11-2021, 09:45 AM
Last Post: Pedroski55
  Data extraction from a table based on column and row names tgottsc1 1 294 Jan-09-2021, 10:04 PM
Last Post: buran
  How i can add elements to table index of element blazej2533 3 254 Dec-03-2020, 08:16 PM
Last Post: Larz60+
  Load data from One oracle Table to Multiple tables amy83 1 216 Dec-02-2020, 01:57 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020