title: Creating SQL Table with variable name from Python
body: <p>I'm trying to create and write a DataFrame to a SQL table using Python. The table should be named after a variable, specifically <code>table_name</code> (<code>table_name</code> will change as I run my code).  Below is the relevant part of my code to try to set this up:</p>
  
  <pre><code>con = sql.connect(r'/Users/linnk/Desktop/Results/Data.db')  # Creates database to write to
  cur = con.cursor() 
  
  ...code...
  
  cur.execute('''CREATE TABLE IF NOT EXISTS ''' + table_name + ''' (Date, Morning1, Day1, Evening1, Night1, Morning3, Day3, Evening3, Night3)''')
  </code></pre>
  
  <p>Running this gives the error (for the last line): </p>
  
  <pre><code>OperationalError: near "-": syntax error
  </code></pre>
  
  <p>I've tried making several modifications to the last line of my code but get similar errors. Can anyone help me spot my mistake/make the necessary adjustments to my code? For information, table_name contains a string.</p>
  
  <p>Thank you in advance for any suggestions.</p>
  
  <p><strong>EDIT/UPDATE:</strong></p>
  
  <p>From what I've read it seems to be better to make a string that is then passed to cur.execute():</p>
  
  <pre><code>   stringexecute='''\'CREATE TABLE IF NOT EXISTS '''+ table_name +''' (Date, Morning1 real, Day1 real, Evening1 real, Night1 real, Morning3 real, Day3 real, Evening3 real, Night3 real)\''''
     cur.execute(stringexecute)
  </code></pre>
  
  <p>For information, stringexecute outputs:</p>
  
  <pre><code>stringexecute= 'CREATE TABLE IF NOT EXISTS GUTUR_400_F1-KIAGA-1 (Date, Morning1Ph real, Day1Ph real, Evening1Ph real, Night1Ph real, Morning3Ph real, Day3Ph real, Evening3Ph real, Night3Ph real)'
  </code></pre>
  
  <p>The code still doesn't work, however. It gives:</p>
  
  <pre><code>OperationalError: near "'CREATE TABLE ......'": syntax error
  </code></pre>
  
  <p>Any further help would be much appreciated. I've primarily looked at and (unsuccessfully) tried the methods from the following resources:
  <a href="http://www.sommarskog.se/dynamic_sql.html#objectnames" rel="nofollow">http://www.sommarskog.se/dynamic_sql.html#objectnames</a>
  <a href="https://docs.python.org/2/library/sqlite3.html" rel="nofollow">https://docs.python.org/2/library/sqlite3.html</a></p>
  
  <p><strong>EDIT</strong></p>
  
  <p>It turns out the following works, as long as the variable <code>table_name</code> does not contain '-' (and possibly other symbols):</p>
  
  <pre><code> cur.execute("CREATE TABLE IF NOT EXISTS " + table_name + " (Date, Morning1 real, Day1 real, Evening1 real, Night1 real, Morning3 real, Day3 real, Evening3 real, Night3 real)")
  </code></pre>