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
  Load data from One oracle Table to Multiple tables amy83 1 58 Today, 01:57 AM
Last Post: Larz60+
  Creating table in MySQL db with decimal number issue dangermaus33 7 156 Nov-20-2020, 10:40 PM
Last Post: dangermaus33
  I try to import data from Excel table to Word Template. NewbiePyPy 0 139 Oct-21-2020, 12:25 PM
Last Post: NewbiePyPy
  Newbie Start with Text or Table? abrogard 5 225 Oct-18-2020, 12:47 PM
Last Post: jefsummers
  How to check spec table in another df Mekala 0 98 Oct-11-2020, 02:25 PM
Last Post: Mekala
  Unable to Update SQLite Table sambanerjee 5 308 Sep-30-2020, 12:21 PM
Last Post: Larz60+
  Fetch Oracle DB rows & print it in HTML file with table's col headers in table format tssr_2001 1 431 Sep-04-2020, 01:39 PM
Last Post: ibreeden
  How to create db table with SQLite and SQLAlchemy?? marcello86 1 424 Sep-02-2020, 03:05 PM
Last Post: marcello86
  Outlook Emails & HTML Table in Message Body JoeDainton123 1 343 Sep-02-2020, 05:15 AM
Last Post: buran
  Adding data to a table in SQLite3 djwilson0495 2 399 Aug-15-2020, 02:48 PM
Last Post: djwilson0495

Forum Jump:

User Panel Messages

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