a SQL statement cannot use a bound parameter as a substitute for SQL 
identifiers, such as table and column names.

that is, this is not valid:

conn.execute("select * from :table", table='foo')

bound parameters are only intended for literal values used in SQL expressions, 
that is, the *data*, not the *structure* of the statement.  This is not a 
SQLAlchemy limitation, it's a fundamental behavior of the DBAPI and database 
client systems in general.   Some DBAPIs might allow arbitrary substitutions, 
but if they do, they're buggy.   

If you have the table name as dynamic, then the approach you have doing the 
substitution using standard Python string techniques is appropriate.



On May 15, 2014, at 5:17 AM, goo...@cornelinux.de wrote:

> Hi,
> 
> my program tries to read data from a configurable, unknown user table.
> So I came up with such a statement:
> 
>         result = self.session.execute("SELECT * FROM :mytable", {"mytable": 
> self.table})
> 
> But when I run it, I get this, what I do not understand:
> 
>     result = self.session.execute("SELECT * FROM :mytable", {"mytable": 
> "wp_users"})
>   File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 
> 791, in execute
>     clause, params or {})
>   File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 
> 1405, in execute
>     params)
>   File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 
> 1538, in _execute_clauseelement
>     compiled_sql, distilled_params
>   File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 
> 1646, in _execute_context
>     context)
>   File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 
> 1639, in _execute_context
>     context)
>   File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 
> 330, in do_execute
>     cursor.execute(statement, parameters)
>   File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in 
> execute
>     self.errorhandler(self, exc, value)
>   File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in 
> defaulterrorhandler
>     raise errorclass, errorvalue
> sqlalchemy.exc.ProgrammingError: (ProgrammingError) (1064, "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 ''wp_users'' at line 1") 'SELECT * 
> FROM %s' ('wp_users',)
> 
> 
> Something like this works out:
> 
>         t = "SELECT * from %s" % self.table
>         result = self.session.execute(t)
> 
> But I do not not not want to do this, since I do not want to check the 
> self.table variable... ;-)
> I am running ubuntu 12.04 with old sqla 0.7.4.
> 
> Thanks a lot and kind regards
> Cornelius
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to