1

How does one use positional / unnamed parameters in SQLAlchemy? This doesn't work.. I know named parameters work but sometimes I prefer unnamed parameters.

engine = sqlalchemy.engine.create_engine('mysql://py:123@localhost/py', echo=True)
con = engine.connect()
res = con.execute(text("select uid, name from users where uid = ?"), 1);
Traceback (most recent call last):
  File "/home/olaf/py/./test.py", line 10, in <module>
    res = con.execute(text("select uid, torrent_pass from xbt_users where uid = ?"), 1);
  File "/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 1011, in execute
    return meth(self, multiparams, params)
  File "/usr/lib/python3/dist-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/lib/python3/dist-packages/sqlalchemy/engine/base.py", line 1090, in _execute_clauseelement
    keys = list(distilled_params[0].keys())
AttributeError: 'list' object has no attribute 'keys'```
1
  • Related, using positional parameters in SQLAlchemy 2.x. Commented Apr 12 at 6:13

2 Answers 2

1

Wrapping the query in text is breaking your code, you need to adjust the syntax. Here is an example from the documentation https://docs.sqlalchemy.org/en/14/core/sqlelement.html?highlight=text#sqlalchemy.sql.expression.text

t = text("SELECT * FROM users WHERE id=:user_id")
result = connection.execute(t, user_id=12)

Below you will find a working version of your code:

>>> from sqlalchemy.sql import text
>>> s = text("select uid, name from users where uid = :uid")
>>> conn.execute(s, uid=1).first() 
Sign up to request clarification or add additional context in comments.

Comments

-2

Try to use like that:

res = con.execute(
    "SELECT uid, name from users where uid = %s", 1)   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.