Delphi + ZeosLib + MySQL + UTF8 HowTo

Home Home
引用 | 編輯 笑笑
2008-06-13 09:02
樓主
推文 x0
最近公司的資料庫要升級為 UTF-8 所以遇到了蠻多的問題,公司的管理系統是用 Delphi 開發的,而資料庫是 MySQL,這幾天好不容易把資料庫由 big5 轉為 utf-8 ,不過讀進來都是亂碼,後來在網路上找到一篇不錯的教學,有用到的可以參考看看

轉貼自

After fiddling around with all these components I finally made a working database connection with my server. It appears to me, that all connections made to MySQL 4.1 default to the "latin1" encoding no matter what the database contains. To work with utf8 you have to tell Zeos to set up certain server variables. You can do this by executing the SQL command: "SET character_set_connection=utf8; SET character_set_results=utf8;" Starting that moment, MySQL expects to get ALL commands in utf8 and will also return all datasets in utf8! You can put these variables into your TZConnection.Properties String list:

character_set_connection=utf8
character_set_results=utf8
Hint: If you want Mysql to use 100% utf8 internally, just add the following to your TZConnection.Properties:

character_set_client=utf8
character_set_connection=utf8
character_set_database=utf8
character_set_results=utf8
character_set_server=utf8
character_set_system=utf8
collation_connection=utf8_general_ci
collation_database=utf8_general_ci
collation_server=utf8_general_ci
Codepage=utf8
Now that MySQL is returning everything in utf8 you have to make sure to use WideString variables ALWAYS.
For NON-BLOB fields you can use the following:

VAR MyString : WideString;
VAR DBString : UTF8String;
[...]
DBString := MyTable.FieldByName( ..

訪客只能看到部份內容,免費 加入會員



獻花 x0
引用 | 編輯 highleekimo
2009-06-24 11:17
1樓
  
有沒有中文教學

英文看不太懂 表情

獻花 x0