0

I am creating dynamically a bulk insert query. In my application I use sqlAlchemy and I would like to execute that insert.

The problem lies when I am creating the query. I want to insert data that contain special characters that should be escaped.

Using MysqlDb module there is a function MySQLdb.escape_string that escapes the special characters but unfortunately does not work for Unicode characters.

What are my options?

I want to notice that it is not easy to create the query as

q = INSERT INTO table (a, b) VALUES (:a1, :b1), (:a2, :b2) 
q_dict = {(a1, b1), (a2, b2)}

so I can execute Session.execute(q, q_dict) .

Is there a python function that accepts a string and escapes all characters that need escapting for Mysql?

Imagine that I want to create write a file to disk and then execute it through mysql console.

1 Answer 1

1

Here is the solution :

First get a DBAPi connection

 connection = engine. raw_connection()

And then you can escape the string v using:

 connection.escape(v)
Sign up to request clarification or add additional context in comments.

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.