views:

158

answers:

1

When I add a relative path to an SQL query as a String all the \ get removed.

I am trying to add the String ("pics\\"+onlyFile) and as you can see I have escaped the \ character so I don't understand why it is being removed. onlyFile is a variable containing a file name.

The value of the "src" variable (the one I am discussing) just before the SQL statement is correct, hence it is something happening when it is added to the SQL query.

+3  A: 

\ is also a MySql escape character, so you need to pass \\ to it.

So, doubling the escaped string should work:

"pics\\\\" + onlyFile

Of course, if onlyFile has escape characters you will need to double escape them too.

Oded
as I was saying))
hgulyan
@hgulyan - "Try this construct which sometimes works" is nowhere near as good an answer as an explanation about why. Explanations trump mysterious voodoo.
David Dorward
haha. very funny. If it worked, I would explain why. I wasn't just sure if it was the problem. Without information about what language is it and DB, I could just guess.
hgulyan
@hgulyan - So, you are happy to post answers that may be completely wrong, but just might be right, in which case you would be kind enough to explain why they are right?
Oded
@Oded, and you're happy to write a solution, that was already written as if it was your idea. Look, I admit, that your answer is better, than mine, it fully describes the problem and the reason, but don't tell me I'm wrong suggesting a solution that is common in this case, especially if it doesn't hurt anything. The reason, why I didn't explain my solution wasn't in kindness or anything, I just wasn't sure, that's it, but suggesting a solution, that can help isn't a crime. I'm not gonna continue to argue, if you think I'm wrong, let's just stop on it. I've nothing more to say.
hgulyan
If you aren't sure, then the explanation about why it might work is even more important. It helps the questioner determine if the solution actually applies to their problem or not.
David Dorward
Thanks to all who answered including hgulyan
Ankur