0

I am working on excel files and database storaging, precisely I am storaging excel data to MySQL database. At some point I am executing this query:

query_for_id = ''' SELECT id FROM attivita WHERE attivita = '{0}' '''.format(attivita)

And when I print the query result I get this:

' SELECT id FROM attivita WHERE attivita = \\'Manutenzione\\' '

When it tries to match 'attivita' with the right value, taken from the excel, I got errors because of the '\\'.

I tried changing the triple quotes from " " " to ' ' ', as well as using connection.escape_string(), but I didn't solve the problem. Can anyone help me figuring out the problem? Thank you in advance.

1 Answer 1

1

I think that in your case better to use arguments to execute

query_for_id = ''' SELECT id FROM attivita WHERE attivita = %(attivita)s '''
cursor.execute(query_for_id, { 'attivita': attivita })

https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html

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.