Both mv and ml will not be recognized, since you haven't defined them as variables.

The second argument of execute statement is a dictionary, and all the elements of your plain query "UPDATE client SET musicVol = :mv , messageVol = :ml" escaped with a colon are being searched for in this dictionary's keys. The execute method did not found a key 'mv' nor 'ml' in this dictionary, therefore an error is raised.

This is the correct version:

db.my_session.execute(
    "UPDATE client SET musicVol = :mv, messageVol = :ml",
    {'mv': music_volume, 'ml': message_volume}
)
Answer from mpiskore on Stack Overflow
🌐
SQLAlchemy
docs.sqlalchemy.org › en › 21 › faq › sqlexpressions.html
SQL Expressions — SQLAlchemy 2.1 Documentation
This functionality is provided ... the raw sql string of a query may prove useful. The above approach has the caveats that it is only supported for basic types, such as ints and strings, and furthermore if a bindparam() without a pre-set value is used directly, it won’t be able to stringify that either. Methods of stringifying all parameters unconditionally ...
🌐
Reddit
reddit.com › r/flask › [af] sqlalchemy: converting a query object to raw sql & parameters and then executing it
r/flask on Reddit: [AF] SQLAlchemy: Converting a query object to raw SQL & parameters and then executing it
April 19, 2018 -

I hope that it is OK to post this question here, although it is not strictly related to Flask. I couldn't find a better place to post it.

Context: Flask, SQLAlchemy and MySQL

I am looking for a way to convert a query object to raw SQL and parameters and save this in a dictionary, in order to be able to pickle dump and load the dictionary before executing the query. How can I achieve converting the query and then executing the text and parameters with SQLAlcehmy?

I'd be eternally grateful if anyone could point me in the right direction.

🌐
Copdips
copdips.com › 2020 › 06 › compiling-sqlalchemy-query-to-nearly-real-raw-sql-query.html
Compiling SQLAlchemy query to nearly real raw sql query - A code to remember
June 8, 2020 - from datetime import date, datetime, timedelta from sqlalchemy.orm import Query def render_query(statement, db_session): """ Generate an SQL expression string with bound parameters rendered inline for the given SQLAlchemy statement. WARNING: This method of escaping is insecure, incomplete, ...
🌐
tutorialpedia
tutorialpedia.org › blog › sqlalchemy-raw-query-and-parameters
SQLAlchemy Raw Queries: How to Properly Use Parameters for Secure Execution — tutorialpedia.org
Migrating legacy SQL code into a SQLAlchemy workflow. To execute raw queries, SQLAlchemy provides the text() construct (from sqlalchemy.text), which parses SQL strings and allows parameter binding.
Find elsewhere
🌐
SQLAlchemy
docs.sqlalchemy.org › en › 20 › faq › sqlexpressions.html
SQL Expressions — SQLAlchemy 2.0 Documentation
This functionality is provided ... the raw sql string of a query may prove useful. The above approach has the caveats that it is only supported for basic types, such as ints and strings, and furthermore if a bindparam() without a pre-set value is used directly, it won’t be able to stringify that either. Methods of stringifying all parameters unconditionally ...
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-execute-raw-sql-in-sqlalchemy
How to Execute Raw SQL in SQLAlchemy - GeeksforGeeks
February 28, 2022 - from sqlalchemy import text # define a tuple of dictionary of # values to be inserted data = ( { "book_id": 6, "book_price": 400, "genre": "fiction", "book_name": "yoga is science" }, { "book_id": 7, "book_price": 800, "genre": "non-fiction", "book_name": "alchemy tutorials" }, ) # write the insert statement statement = text("""INSERT INTO BOOKS (book_id, book_price, genre, book_name) VALUES(:book_id, :book_price, :genre, :book_name)""") #insert the data one after other using execute # statement by unpacking dictionary elements for line in data: engine.execute(statement, **line) # write the SQ
🌐
DEV Community
dev.to › spaceofmiah › database-interaction-with-sqlalchemy-raw-dml-dql-4bc8
Database Interaction with SQLAlchemy - Raw DML & DQL - DEV Community
October 19, 2022 - SQLAlchemy recommends executing an external source (e.g human user, automation process) data using named parameter syntax denoted with :any_given_name when using textual construct.
🌐
Atlassian
atlassian.com › data › notebook › how to execute raw sql in sqlalchemy
How to Execute Raw SQL in SQLAlchemy | Atlassian
This tutorial offers a practical approach to executing raw SQL queries in SQLAlchemy, providing clear examples and tips for efficient database management.
🌐
Sentry
sentry.io › sentry answers › flask › execute raw sql in flask-sqlalchemy application
Execute raw SQL in Flask-SQLAlchemy application | Sentry
March 15, 2024 - It is possible to achieve the same ... product_category="Fruit") Parameter binding allows us to reuse the same SQL query with different values and mitigates the risks of SQL injection when working with untrusted data....
🌐
ZetCode
zetcode.com › db › sqlalchemy › rawsql
Raw SQL in SQLAlchemy - ZetCode
With the connection's execute method, we deliver a simple SELECT SQL statement. ... With the fetchone method, we retrieve a single row. From this row, we get the scalar value. ... The value is printed to the console. ... We execute the script. In the next example, we connect to the PostgreSQL database and print its version. ... #!/usr/bin/python # -*- coding: utf-8 -*- from sqlalchemy import create_engine eng = create_engine('postgresql:///testdb') con = eng.connect() rs = con.execute("SELECT VERSION()") print rs.fetchone() con.close()
🌐
SQLAlchemy
docs.sqlalchemy.org › en › 14 › core › tutorial.html
SQL Expression Language Tutorial (1.x API) — SQLAlchemy 1.4 Documentation
Above, while the values method limited the VALUES clause to just two columns, the actual data we placed in values didn’t get rendered into the string; instead we got named bind parameters. As it turns out, our data is stored within our Insert construct, but it typically only comes out when the statement is actually executed; since the data consists of literal values, SQLAlchemy automatically generates bind parameters for them.
🌐
MoldStud
moldstud.com › articles › developers faq › python web developers questions › unlocking advanced sqlalchemy features - master raw sql queries and more
Unlocking Advanced SQLAlchemy Features - Master Raw SQL Queries and More
June 16, 2025 - For example: from sqlalchemy import ... tuples for added flexibility. Always prefer parameterized placeholders (:param) over manual string formatting to avoid SQL injection vulnerabilities....
🌐
TutorialsPoint
tutorialspoint.com › sqlalchemy › sqlalchemy_core_using_textual_sql.htm
SQLAlchemy Core - Using Textual SQL
from sqlalchemy.sql import text ... students.lastname from students where students.name between ? and ? The values of x = A and y = L are passed as parameters. Result is a list of rows with names between A and L − ·...