1. 程式人生 > >使用mysqldump匯出資料時對欄位中包含的單引號的處理

使用mysqldump匯出資料時對欄位中包含的單引號的處理

最近在做一個日誌統計專案,有一個輔助表是在MySQL資料庫的,現在要將其遷移到Postgresql,自然是先用mysqldump將MySQL裡面的資料匯出,然後再匯入到Postgresql即可。但在實際操作過程中,發現一些欄位中本身就含有單引號,在匯出的sql語句中,對這樣的單引號使用’\’做了轉義處理,比如原來的某個欄位的值為:It’s very popular,匯出的sql中大概是這樣的格式:’It\’s very popular’,可以看到,欄位中的單引號被轉義。但使用匯出的sql往Postgresql中插入資料時,卻報告錯誤,因為Postgresql對’\’作為轉義字元並不識別。

Question:


I use mysqldump to backup my mysql database. The problem is that the sql file generated by mysqldump doesn’t escapes single quotes properly.

Here is an example of the mysqldump generated sql script :

INSERT INTO someTable VALUES (1,’This ain\’t escaped correctly’);

That single quote escaping in “ain’t” doesn’t work and it makes the rest of the script being inside that string. Is there a way around this?

Answer:
That output of mysqldump is working as designed, and it is properly escaped, unless you try to restore the dump on a MySQL instance with SQL_MODE=NO_BACKSLASH_ESCAPES set.

There’s an outstanding feature request to make mysqldump use a pair of single-quotes to escape literal single-quotes, as per ANSI SQL. See

http://bugs.mysql.com/bug.php?id=65941

In the meantime you might be able to convert from backslash-singlequote to pair-of-singlequotes with a command line this:

mysqldump test | sed -e “s/\\’/”/g” > test-dump.sql

I tried that out briefly by creating a dummy table in my test database and inserting the string “O’Hare” into the table. But that’s hardly a comprehensive test – I take no responsibility for this suggestion working in all cases.

結合另外一篇部落格,完整的匯出命令應寫成:

mysqldump -u root -ppassword –no-create-db –no-create-info
–complete-insert –compatible=mssql –default-character-set=utf8 –skip-opt –compact –extended-insert=false dbname tablename|sed “s/\\’/”/g”>tablename.sql

引數的含義如下:

–no-create-db 不輸出建database的指令碼
–no-create-info 不輸出建立table的指令碼
–complete-insert 完整的插入,輸出每個欄位(如: insert into table(field1,field2,….) values(value1,value2,…))
–compatible=mssql 教本相容格式,這裡是mssql 這樣教本里就會把table的名字和欄位名用“號引起來,而不是mssql不能識別的`號。
–default-character-set=utf8 預設編碼
–compact 輸出儘量少的資訊
–extended-insert=false 禁用它,可以每行生成一句insert語句,否則只輸出一個。

至此,問題解決。在Postgresql中,兩個單引號表示一個單引號,因此在匯出時使用sed命令直接將\’替換成”,就可以將資料正確插入到Postgresql中了。