Issue
I am connected to postgresql with sqlalchemy. When I try this code:
e = create_engine('')
r = e.execute("select ?", 5)
it gives me:
sqlalchemy.exc.ProgrammingError: (ProgrammingError) syntax error at end of input
Corresponding to http://www.sqlalchemy.org/docs/core/connections.html?highlight=engine#sqlalchemy.engine.base.Connection.execute there are different types of paramstyles... After deeper research, i found out that default paramstyle for postgresql Dialect is "pyformat".
Can somebody show me how to use this pyformat on some example? I would just like to use placeholders or named placeholders for forming up the sql statement.
I tried:
e.execute("select %s, %s;", "test", "test2")
But this is not working either.
Thank you
edit: of course, i am passing valid connection string to create_engine method :-)
Solution
The documentation you link to, directly links to PEP 249, which shows you the different styles.
About pyformat it says:
'pyformat' Python extended format codes, e.g. '...WHERE name=%(name)s'
So that's what you need to use.
By Googling on "DBAPI pyformat" the second link is the link to the psycopg2 documentation. After mentioning that it uses pyformat there is a "See Also" linking to a page with loads of examples.
Examples of how to use it are in your original link:
e.execute("select %s, %s;", ("param1", "test"), ("param2", "test2"))
You can also use this syntax:
e.execute("select %(param1)s, %(param2)s;", param1="test", param2="test2"))
Which is nicer.
Answered By - Lennart Regebro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.