BigSea'Blog

身体下地狱,眼睛上天堂,心灵回故乡。走在路上,那是痛苦的幸福;走过之后,是幸福的痛苦。

utf-8

文件批量转换器(从ansi 到 Utf-8)

No Comments | 安全技术 | by bigsea | 6375 Views. | 2007, December 11, 12:45 AM

文件批量转换器(从ANSI 到 utf-8)(PHP)
其中几个函数很好用,我花了一天时间
直接创建多层目录mymkdir($dirname),
批处理目录下的文件function listDirTree( $dirName = null ,$callFunction) 可自定义处理函数。
如:

PHP 代码:
$callFunc="backupFile(\"$backupPath\",\$file,\$filePath,\$dirName);";
listDirTree($operateDir,$callFunc); 
 
将文本中 x=y
u=a
提取为数组的函数
function myExplode($originalStr,$firstTag,$secondTag)
将文件转换为utf-8编码的函数。function gb2utf8($file,$filePath)
判断文件是否为utf-8编码的函数 function utf8_probability(&$rawtextstr)

» 阅读全文

阅读全文

数据库转字符集后出现的表增量丢失问题

No Comments | 我的文章 | by bigsea | 2498 Views. | 2007, December 9, 5:02 PM

前期服务器由于mysql4.1采用默认编译存储 latin1 字符集

在数据导出导入在默认编码utf-8环境中出现乱码情况

前天终于下定决心把数据库从latin1转换为utf-8,转换很顺利

可以正常显示中文汉字,但是在搜索和添加新文章时出现mysql错误

数据库出错:

MySQL Query Error
Mysql error description: Duplicate entry '0' for key 1
Mysql error number: 1062
Date: 2007-12-09 @ 06:36
http://www.XXX.com/admin/admincp.php?job=article

错误原因为在从latin1到utf-8转换过程中部分表属性丢失自动增量属性

解决办法:需要找到出错页面调用的数据库中的表和字段,这一步比较麻烦,需要在php代码中查询到操作的表;

[attach=0]

先把默认值改删除留空并选择增量属性后保存;否则会提示错误

解决过程中比较麻烦的就是找到出错的表的

在这里感谢Tice

ziduan.jpg

ziduan.jpg - 大小: 20.57 K - 尺寸:  x  - 点击打开新窗口浏览全图

阅读全文

Convert latin1 to UTF-8 in MySQL

No Comments | 安全技术 | by bigsea | 4047 Views. | 2007, December 6, 11:00 PM

Convert latin1 to utf-8 in mysql
From Gentoo Linux Wiki
Jump to: navigation, search
[edit]latin1 to UTF-8 in MySQL
Since MySQL 4.1, UTF-8 is the default charset. If you have an old database, containing data encoded in latin1, and you want upgrade to a newer MySQL server, than you have to do the following:

MySQL dump

First of all, we need to dump the old data into a file.




Code: Create a MySQL dump
$ mysqldump -h host.com --user=frog -p --default-character-set=latin1 -c \
--insert-ignore --skip-set-charset dbname > dump.sql




Please mention, that you have to replace the user, the host and the dbname, otherwise it will result in an error :)


Convert dump

Next thing to do is, converting the characters in the MySQL dump from latin1 to UTF-8




Code: Convert dump
$ iconv -f ISO-8859-1 -t UTF-8 dump.sql > dump_utf8.sql




If you have another source charset, you need to replace the -f option with your local character set.


Drop and create

Now it's time to drop the old database and create a new one with UTF-8 support.




Code: Drop and Create
$ mysql --user=frog -p --execute="DROP DATABASE dbname;
CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;"




Import dump to databse

Last but not least, we need to import the converted data back to the new database.




Code: Import dump
$ mysql --user=frog --max_allowed_packet=16M -p --default-character-set=utf8 dbname < dump_utf8.sql




The max_allowed_packet-option is sometimes important. If your import ends up with a "ERROR 1153 at line 42: Got a packet bigger than 'max_allowed_packet'", you need to increase the packet size. Please mention, that you also need to update /etc/mysql/my.cnf and set max_allowed_packet=16M under the [mysqld] directive

Retrieved from "http://gentoo-wiki.com/TIP_Convert_latin1_to_UTF-8_in_MySQL"

阅读全文

mysql编码的2篇文章,备日后查用

4 Comments | Unix/Linux技术 | by bigsea | 9259 Views. | 2007, November 29, 6:32 PM

如何修正 MySQL ?料?的 encoding?

最近 gslin 的 blog 因? MySQL ?料?的 encoding ??,?生?常?象,只好?零?始。??有偶地,Pesty 最近也?理了 MySQL 使用 utf-8 的相???。

?些??之所以??生,是因?越?越多的 web application 套件使用 UTF-8 ?存?料,但大家在?定 MySQL ??都?有注意到??,仍然使用??的 latin1 作??料?的 encoding。Web application ?入 UTF-8 ?料,MySQL ??作 latin1 在?,?然 web application ?回?的?候,因??好是反向?理,?料看似?有任何?常,但??上 collation 是?的,??行移?、?份甚至?? web application ?,也都??生??。

我一年多前也有遇到?似的??,原本在公司有?用 phpBB2 架的 forum,?用 big5 ??,但??上 mysql-3.23 ?是用 latin1 存。想要??成 UTF-8,移?到 FreeBSD 5 上改用 mysql-4.1,就?生??了。我那??有解?,所以 phpBB2 移?????了一年多,但?在是多跑一? virtual machine 只?了??移不??的 phpBB2,?在浪????。?好看到 gslin ? pesty 最近也在弄????,就重新?了一遍步?如下:

用 mysqldump 自 mysql-3.23 倒出?,??必?加上 --default-character-set=latin1 的??,才能正?倒出??上是用 big5 的 phpBB2 的?料?。
用 iconv -c -f CP950 -t UTF-8 ?倒出?的 SQL ?成 UTF-8。必?要加 -c,否??死在半路。?句??,其?中?已?有?西?掉了。
? mysql 用 SET GLOBAL/SESSION character_set_XXX=utf8 ?整 system variables,? charset ? collation 通通改成 UTF-8,然後做 CREATE DATABASE phpbb2。
再用 SOURCE 指令把??用 iconv ?出?的 SQL ?倒回?。
?果在第四?步??,mysql (client) 就 core dump 了。猜?有可能是第二步用 -c 硬??,把某?指令搞?了。不? mysql (client) 是直接 core dump 而不是跑 error message 出?指出??何在,所以?下??了,只能靠 iconv 不加 -c 想?法慢慢抓??。

» 阅读全文

阅读全文