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
Photo Converting Pandas DataFrame to a table of hourly blocks Abedin 1 576 Apr-24-2025, 01:05 PM
Last Post: snippsat
  Pinball table CCC fourbanks 0 422 Mar-09-2025, 03:10 PM
Last Post: fourbanks
  working with pivot table gunther 1 431 Jan-22-2025, 08:55 AM
Last Post: Keville_35
  Convert Json to table format python_student 4 14,865 Dec-05-2024, 04:32 PM
Last Post: Larz60+
  Extracting table and table name from PDF vinibhat 4 7,646 Aug-10-2024, 07:29 AM
Last Post: Pedroski55
  drawing a table with the status of tasks in each thread pyfoo 3 1,449 Mar-01-2024, 09:29 AM
Last Post: nerdyaks
  How to create a table with different sizes of columns in MS word pepe 8 8,336 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 3,418 Oct-06-2023, 04:32 PM
Last Post: cubangt
  Mysql and mysql.connector error lostintime 2 1,721 Oct-03-2023, 10:25 PM
Last Post: lostintime
  dict table kucingkembar 4 1,823 Sep-30-2023, 03:53 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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