SQLAlchemy: How to change a MySQL server system variable using SQLAlchemy?

Question:

I want to set the general_log and general_log_file variables using SQLAlchemy, is there a way to do this? I’ve been Googling around and can’t find anything on the topic.

Asked By: HLH

||

Answers:

You can execute any raw SQL query which you need (of course you have to get appropriate rights in the session). To change a variable run something like this:

# change variable name and values to what you need
connection.execute("SET SESSION query_cache_type = OFF")
Answered By: Aleksandr K.

As mentioned previously, you could use the following code the set a variable using the raw Connection object.

connection.execute("SET SESSION query_cache_type = OFF")

If you have a Session object, you can retrieve the underlying Connection object using the Session.connection() function.

So your code could look as follows.

session.connection().execute("SET SESSION query_cache_type = OFF")
Answered By: Hugh Han
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.