共計 4114 個字符,預計需要花費 11 分鐘才能閱讀完成。
這篇文章給大家介紹如何進行 MYSQL 特殊字符的處理,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
單引號,以及行尾的斜杠引起的困惑:
這一次的問題,我們直接從實際的工作中說起:
工作內容簡介:有一批用戶 ID 存在文件里,需要從數據庫里刪除?
做這個事情,可能有很多的方法:
1,把 ID 導入到數據庫中,用 SQL 直接做表關聯去刪除;
2,用 SHELL(或其他語言)寫個小程序,根據文件里的 ID 做一個 FOR 循環,然后在 MYSQL 中去刪除;
3,用 sed 直接把 ID 轉成 delete 語句,完了直接運行即可;
[@more@]
由于數據量較大(1.6 億),顯然,我會用偷懶以及簡單的方法 3:
———————————————-
[root@im_ctuallot1 tmp]# cat loginid.txt
xouqun76818
ogku15mtb7c
jinlongkaikai@163.com
曾樸紹 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 long_id= jinlongkaikai@163.com
delete from ctulog.db_allot_center_64 where long_id= 曾樸紹 283902
delete from ctulog.db_allot_center_64 where long_id= 輕舞飛揚 061129 付了
mysql -uroot -h227.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
jinlongkaikai@163.com
曾樸紹 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 long_id= jinlongkaikai@163.com
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 -h227.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 中去執行:
root@127.0.0.1 : ctulog 15:59:04
root@127.0.0.1 : ctulog 15:59:04 delete from ctulog.db_allot_center_64 where long_id= xouqun76818
Query OK, 0 rows affected (0.00 sec)
root@127.0.0.1 : 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 long_id= jinlongkaikai@163.com
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
root@127.0.0.1 : ctulog 15:59:05 delete from ctulog.db_allot_center_64 where long_id= 輕舞飛揚 061129 付了
Query OK, 0 rows affected (0.00 sec)
root@127.0.0.1 : ctulog 15:59:10
只有第一條,最后一條執行成功了;
到這里我想大家應該明白了,
最關鍵的是單引號的不匹配導致 MYSQL 不能正確認識完整的 SQL;
注意,在用戶 ID 中,出現 單個單引號,或以 結束,都會有這個問題;
問題找到了,其實解決很簡單,就是先把用戶 ID 中的單引號和 $ 作一個轉換:
[root@im_ctuallot1 tmp]# cat loginid.txt
xouqun76818
bao pijkl
ogku15mtb7c
jinlongkaikai@163.com
曾樸紹 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 long_id= jinlongkaikai@163.com
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 一下以免大家在這里浪費時間。
關于如何進行 MYSQL 特殊字符的處理就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。