1

Code where the problem occurs:

    def updateEmpresaSetXEqualsToByCnpj(self, nomeColuna, valor, cnpj):
        query = (r"UPDATE empresa_cliente SET {0} = {1} WHERE cnpj_emp = '{2}';".format(nomeColuna, valor, cnpj))

I'm sending a string, a int and a string. The last param must have single quotes cause SQL SERVER syntax but the python is putting double back slash before each single quotes. e.g.

UPDATE empresa_cliente SET regime_tributacao_federal = 1 WHERE cnpj_emp = \\'33333222000111\\';

When the correct query should be

UPDATE empresa_cliente SET regime_tributacao_federal = 1 WHERE cnpj_emp = '3333222000111';
Junior W.
  • 11
  • 3
  • 1
    Please give us the original values of those variables – NinjaKitty Jul 11 '17 at 20:01
  • 1
    how are you passing the value for cnpj? – Sriram Sitharaman Jul 11 '17 at 20:05
  • So you close single quotes before the value, prematurely closing quotes. It is likely trying to save you from yourself.... – Dan Jul 11 '17 at 20:05
  • @xNinjaKittyx the values of the vars are *regime_tributacao_federal*, *1* and *3333222000111* respectively – Junior W. Jul 11 '17 at 20:05
  • Just using string handling, it works fine for me, tested on 3.6 and 2.7. Try printing the value of `query` immediately after you construct it. – cdarke Jul 11 '17 at 20:17
  • @Dan, I'm passing the values from here `EliteDB.updateEmpresaSetXEqualsToByCnpj('regime_tributacao_federal', regime_tributario, empresa.cnpj)` where **empresa.cnpj** is a string like **3333222000111** – Junior W. Jul 11 '17 at 20:18
  • @cdarke, I did it and and the value printed was: `UPDATE empresa_cliente SET regime_tributacao_federal = 1 WHERE cnpj_emp = '3333222000111';` what is right, but looking for Pycharm Debug the value continues with \\ before each **'** consequently the query is not executed – Junior W. Jul 11 '17 at 20:37
  • 1
    Maybe its a feature of PyCharm then? – cdarke Jul 11 '17 at 20:39
  • @cdarke I think it is. I posted an answer to explain more. Perhaps OP is doing some extra formatting too? – GIZ Jul 11 '17 at 20:53
  • If your query fails then it is for some other reason, it appears you are being distracted by the way that Pycharm is displaying your string in Debug. See https://stackoverflow.com/questions/36384471/why-does-pycharm-use-double-backslash-to-indicate-escaping and https://intellij-support.jetbrains.com/hc/en-us/community/posts/207000145-Escape-Quote-Placed-Automatically-in-String-Why- – cdarke Jul 11 '17 at 21:26
  • 1
    @cdarke you're right. I was distracted by Pycharm Debug. My real problem was on another part of code. I wasn't used to use `commit()` sentence after `execute(query)` sentence. Thx. – Junior W. Jul 12 '17 at 19:14

1 Answers1

2
r"UPDATE empresa_cliente SET {0} = {1} WHERE cnpj_emp = '{2}';"

r"" suspends escape sequences. If you're working interactively and using r"" don't worry about the double \\ when you write to files, it writes only one \ check that with print.

For the second string:

'UPDATE empresa_cliente SET regime_tributacao_federal = 1 WHERE cnpj_emp = \\\'33333222000111\\\';'

Here's what you've not probably noticed:

>>> "\\\'wwww"
"\\'wwww"
>>> print("\\\'wwww")
\'wwww

If you would, change it to this, it's essentially the same maybe aesthetically better:

"UPDATE empresa_cliente SET regime_tributacao_federal = 1 WHERE cnpj_emp = '33333222000111';"

It's also possible that you're passing you strings to another formatting before you write them elsewhere, check for that.

GIZ
  • 4,409
  • 1
  • 24
  • 43