MYSQL特殊字元(單引號,行尾斜槓)的處理
單引號,以及行尾的斜槓引起的困惑:
這一次的問題,我們直接從實際的工作中說起:
工作內容簡介: 有一批使用者ID存在檔案裡,需要從資料庫裡刪除?
做這個事情,可能有很多的方法:
1, 把ID匯入到資料庫中,用SQL直接做表關聯去刪除 ;
2, 用SHELL(或其他語言)寫個小程式,根據檔案裡的ID做一個FOR 迴圈,然後在MYSQL中去刪除 ;
3, 用sed直接把ID轉成delete語句,完了直接執行即可;
由於資料量較大(1.6億),顯然,我會用偷懶以及簡單的方法3 :
----------------------------------------------
[root@im_ctuallot1 tmp]# cat loginid.txt
xouqun76818
ogku15mtb7c
曾樸紹283902
輕舞飛揚061129付了
[root@im_ctuallot1 tmp]# sed -e "s/^/delete from ctulog.db_allot_center_64 where long_id='/g" -e "s/$/';/g" loginid.txt > loginid.sql
[root@im_ctuallot1 tmp]# cat loginid.sql
delete from ctulog.db_allot_center_64 where long_id='xouqun76818';
delete from ctulog.db_allot_center_64 where long_id='ogku15mtb7c';
delete from ctulog.db_allot_center_64 where ;
delete from ctulog.db_allot_center_64 where long_id='曾樸紹283902';
delete from ctulog.db_allot_center_64 where long_id='輕舞飛揚061129付了';
mysql -uroot -h127.0.0.1 --default-character-set=latin1 --force ctulogdb < loginid.sql
--force 是防止某個SQL出現錯誤,而導致整個任務終止;
---------------------------------------------
搞定。
這看似非常簡單的方法,也暴露出很多的問題,結果1.6行資料只成功刪除了3300W,任務失敗;
其實是我把這個任務想得太簡單了: 在使用者ID中存在任何可能的字元 ,如:
bao'pijkl
tingting831118
注意,在使用者ID中有" ' ", 在行末尾有:" ";
我們把這樣的語句滲雜到其他ID中,我們看會有怎麼的效果;
[root@im_ctuallot1 tmp]# cat loginid.txt
xouqun76818
bao'pijkl
ogku15mtb7c
曾樸紹283902
tingting831118
輕舞飛揚061129付了
[root@im_ctuallot1 tmp]# sed -e "s/^/delete from ctulog.db_allot_center_64 where long_id='/g" -e "s/$/';/g" loginid.txt > loginid.sql
[root@im_ctuallot1 tmp]# cat loginid.sql
delete from ctulog.db_allot_center_64 where long_id='xouqun76818';
delete from ctulog.db_allot_center_64 where long_id='bao'pijkl';
delete from ctulog.db_allot_center_64 where long_id='ogku15mtb7c';
delete from ctulog.db_allot_center_64 where ;
delete from ctulog.db_allot_center_64 where long_id='曾樸紹283902';
delete from ctulog.db_allot_center_64 where long_id='tingting831118';
delete from ctulog.db_allot_center_64 where long_id='輕舞飛揚061129付了';
[root@im_ctuallot1 tmp]# mysql -uroot -h127.0.0.1 --default-character-set=latin1 --force ctulog < loginid.sql
ERROR at line 2: Unknown command '''.
ERROR 1064 (42000) at line 2: 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 'pijkl';
delete from ctulog.db_allot_center_64 where long_id='ogku15mtb7c';
delet' at line 1
會出現一堆這樣的錯誤;
如果你手動把以上SQL貼到MYSQL中去執行:
: ctulog 15:59:04>
: ctulog 15:59:04> delete from ctulog.db_allot_center_64 where long_id='xouqun76818';
Query OK, 0 rows affected (0.00 sec)
: ctulog 15:59:05> delete from ctulog.db_allot_center_64 where long_id='bao'pijkl';
'> delete from ctulog.db_allot_center_64 where long_id='ogku15mtb7c';
'> delete from ctulog.db_allot_center_64 where ;
'> delete from ctulog.db_allot_center_64 where long_id='曾樸紹283902';
'> delete from ctulog.db_allot_center_64 where long_id='tingting831118';
ERROR:
Unknown command '''.
ERROR 1064 (42000): 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 'pijkl';
delete from ctulog.db_allot_center_64 where long_id='ogku15mtb7c';
delet' at line 1
: ctulog 15:59:05> delete from ctulog.db_allot_center_64 where long_id='輕舞飛揚061129付了';
Query OK, 0 rows affected (0.00 sec)
: ctulog 15:59:10>
只有第一條,最後一條執行成功了;
到這裡我想大家應該明白了,
最關鍵的是單引號的不匹配導致MYSQL不能正確認識完整的SQL;
注意,在使用者ID中,出現 單個單引號,或以""結束,都會有這個問題;
問題找到了,其實解決很簡單,就是先把使用者ID中的單引號和"$"作一個轉換:
[root@im_ctuallot1 tmp]# cat loginid.txt
xouqun76818
bao'pijkl
ogku15mtb7c
曾樸紹283902
tingting831118
輕舞飛揚061129付了
[root@im_ctuallot1 tmp]# sed -e "s///g" -e "s/'/'/g" -e "s/^/delete from ctulog.db_allot_center_64 where long_id='/g" -e "s/$/';/g" loginid.txt > loginid.sql
[root@im_ctuallot1 tmp]# cat loginid.sql
delete from ctulog.db_allot_center_64 where long_id='xouqun76818';
delete from ctulog.db_allot_center_64 where long_id='bao'pijkl';
delete from ctulog.db_allot_center_64 where long_id='ogku15mtb7c';
delete from ctulog.db_allot_center_64 where ;
delete from ctulog.db_allot_center_64 where long_id='曾樸紹283902';
delete from ctulog.db_allot_center_64 where long_id='tingting831118';
delete from ctulog.db_allot_center_64 where long_id='輕舞飛揚061129付了';
[root@im_ctuallot1 tmp]#
是這樣的SQL執行就不會有任何問題。
就這麼簡單的問題,耗了5個小時,還是SHARE一下以免大家在這裡浪費時間;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/703656/viewspace-1036578/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 特殊字元——反斜槓(\)字元
- shell中單引號、雙引號、反引號、反斜槓的區別
- 特殊字元的處理。。¥$$$字元
- XAML中的特殊符號幾空白字元處理符號字元
- SQL特殊字元處理zzSQL字元
- mysql中建立包括特殊字元的帳號MySql字元
- 批處理特殊符號 萬用字元與轉義符符號字元
- java replaceall 用法:處理特殊字元Java字元
- PHP對錶單提交特殊字元的過濾和處理PHP字元
- 徹底理解斜槓和反斜槓的區別
- excel斜槓分割表格怎麼做 excel單元格斜槓拆分上下打字Excel
- 含有特殊字元的資料檔案處理字元
- URL路徑中包含特殊字元%的處理字元
- 字串中包含單引號和雙引號怎麼處理字串
- 引號的處理方式
- 單引號在動態SQL中的處理SQL
- Oracle sql 語句中帶有特殊的字元處理OracleSQL字元
- 正反斜槓的區別
- mybatis的mapper的特殊符號處理MyBatisAPP符號
- PHP關於反斜槓處理函式addslashes()和stripslashes()的用法PHP函式
- 【問題處理】“sqlplus / as sysdba”無法登陸——多餘的斜槓!SQL
- vue元件中,編譯後的檔案斜槓變成了反斜槓。Vue元件編譯
- 關於特殊符號&與& 的處理符號
- MySQL:簡單記錄訊號處理MySql
- Java-反斜槓Java
- 處理檔名內含有特殊字元的檔案 (轉)字元
- ls -ld 符號連結 加上斜槓輸出不同符號
- oracle中斜槓(/)的含義Oracle
- swift中webview的URL帶中文或者特殊字元的處理SwiftWebView字元
- Mysql單引號使用MySql
- 元字元為名稱的時候,使用兩個反斜槓轉義:\\字元
- mysql替換特殊字元MySql字元
- 欄位處理rtrim去掉結尾的特殊字元和空格字元
- MySQL 處理行號MySql
- 正規表示式匹配轉義字元反斜槓程式碼例項字元
- 萬用字元與特殊符號字元符號
- php插入資料含有特殊符號的處理方法PHP符號
- Linux 下如何處理包含空格和特殊字元的檔名Linux字元