2017-02-28 171 views
0

我寫一個方法來存儲導航字符串,但該字符串是這樣的:如何在數據庫中存儲「」?

"data\510000RS\Other\" 

因此,它是崩潰:

[SQL] call P_A_OperReg_Bio("liaomalin", "511321199001031598", "P_1", "http:p_1......", "http://p1_url1..", "md5_p1_md51", "http://p1_url2..", "md5_p1_md52", "http://p1_url3..", "","data\510000RS\Other\", "20170202", "", @a); 
SELECT @a; 
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '20170202", "", @a); 
SELECT @a' at line 1 

快照:

enter image description here

如何解決這個問題?


如果我有字符串:

data\510000RS\Other\ 

我怎樣才能把它轉換爲:

"data\\510000RS\\Other\\" 
+1

,改成'轉義\\\'。 –

+0

@Steve Smith,如何將「\」轉換爲「\\」?如果我有字符串(「data \ 510000RS \ Other \」) – lme

+0

你以什麼方式「擁有」那個字符串?請注意''data \ 510000RS \ Other \「'不是一個有效的java表達式。是你的*實際*字符串數據\ 510000RS \其他\? – luk2302

回答

2

你需要逃避,如果它面前的另一個斜線:

「data \\ 510000RS \\ Other \\」

如果它在一個程序中,不要這樣做,這是不安全的!使用預準備的語句在JAVA

實施例:https://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

+0

如何將「data \ 510000RS \ Other \」轉換爲「data \\ 510000RS \\ Other \\」? – lme

+0

@lme在哪種編程語言? – Tom

+0

我使用java @Tom – lme

0

反斜線符號被用作數據塊轉義字符。 您可以使用兩個反斜槓,而不是一個用於生成結果或插入記錄。

"data\\510000RS\\Other\\" 

Insert into table Values(1, "data\\510000RS\\Other\\" 
0

正如有人指出,你可以通過轉義用雙斜槓替換所有slasshes的slah(\)解決它。

我建議使用的apache.commons.lang

org.apache.commons.lang; 

StringEscapeUtils.escapeSql(String str) 

這種方法不僅會逃脫斜線也是其它字符造成的問題,同時插入DB

相關問題