您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何進行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特殊字符的處理就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。