MYSQL特殊字元(單引號,行尾斜槓)的處理

Steven1981發表於2010-08-10

單引號,以及行尾的斜槓引起的困惑:

這一次的問題,我們直接從實際的工作中說起:

工作內容簡介: 有一批使用者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

曾樸紹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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章