Some string contains quotes and other characters (aOE "has fallen), as well as text with Russian characters:

strinnng = "аоЭ\"пал" 

I try to screen these signs (there is such a function in the _mysql_connector connector). The output is a byte string. Why then what to do with it? I get errors = ((((

 tyu = cnx.escape_string(strinnng) 

Then I make a request in the database

 cnx.query('INSERT INTO test (title) VALUES ("%s")' % tyu) 

Help who knows?

    2 answers 2

    Screening sql queries has its own pitfalls, and if you do it by hand, you can spend a lot of time solving such problems.

    It is better to use sqlalchemy: it also knows how to perform raw queries bypassing ORM, while shielding them. An example from the documentation .

    And even better to use ORM - completely abstract from working with the sql query. An ORM tutorial from the sqlalchemy documentation .

       bytes.decode(encoding="utf-8", errors="strict") 

      This is to convert to string using appropriate encoding. Generally, escaping strings: r'str' .

      Or did I misunderstand you?

      Additional information .