title: SQLAlchemy: using variable table name
body: <p>I need to perform SQL statements using SQLAlchemy with a variable table name (come from an external source).
  I tried several options but no one was successful.  </p>
  
  <p>Attempt 1: With <code>text()</code>:</p>
  
  <pre><code>def empty_table(table_name,testf):
          with db.connect() as conn:
              s = text("SELECT * FROM $table_name")
              rp = conn.execute(s) 
  </code></pre>
  
  <p>receive: </p>
  
  <blockquote>
    <p>sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax
    error at or near "$" LINE 1: SELECT * FROM $table_name</p>
  </blockquote>
  
  <p>Attempt 2: With <code>select()</code>:</p>
  
  <pre><code>with db.connect() as conn:
              s = select([table_name]) 
              rp = conn.execute(s)
  </code></pre>
  
  <p>receive:</p>
  
  <blockquote>
    <p>sqlalchemy.exc.ArgumentError: Textual column expression
    'my_test_table' should be explicitly declared with
    text('my_test_table'), or use column('my_test_table') for more
    specificity</p>
  </blockquote>
  
  <p>I found in <a href="https://stackoverflow.com/questions/41176006/how-to-do-a-sqlalchemy-query-using-a-string-variable-as-table-name">How to do a sqlalchemy query using a string variable as table name?</a> that it possible to resolve the problem using eval(my_variable) but there is no detailed explanation.</p>