mysql的中文問題
perl2編程mysql中文 [天氣] 2005年9月13日11:14星期二 [Dev開發] 追蹤此文的RSS
使用connect方法建立一個到數據源的連接。$data_source應該以DBI:driver_name:開始,例如:
$dbh = DBI->connect("DBI:mysql:$database", $user, $password); www.shengfang.org
$dbh = DBI->connect("DBI:mysql:$database:$hostname",$user, $password);
$dbh = DBI->connect("DBI:mysql:$database:$hostname:$port",$user, $password);
Can't locate DBI.pm in at INC (@INC contains: O:/php/perl/lib O:/php/perl/site/lib dot ) at O:\php\OptiPerl\webroot\cgi-bin\newscript1.cgi line 3.
BEGIN failed--compilation aborted at O:\php\OptiPerl\webroot\cgi-bin\newscript1.cgi line 3.
#!O:/php/perl/bin/Perl.exe www.shengfang.org
print "Content-type: text/plain; charset=gb2312\n\n";
print "rrow[1]\n";
use DBI; www.shengfang.org
my $dbh = DBI->connect('dbi:mysql:astbill:localhost','root','123');
$dbh->do("set names gbk");
my $sql = "insert into admin(namee,pwd) values('對數函數','閃電劃過的')";
my $sth = $dbh->prepare($sql);
$sth->execute(); www.shengfang.org
my $sql = "select * from admin";
my $sth = $dbh->prepare($sql);
$sth->execute(); www.shengfang.org
my at row;
while(@row=$sth->fetchrow_array())
{ www.shengfang.org
print "$row[2]\n"; www.shengfang.org
}
rrow[1]
21232f297a57a5a743894a0e4a801fc3
?????
rrow[1] www.shengfang.org
21232f297a57a5a743894a0e4a801fc3
閃電劃過的 www.shengfang.org
[client]
port=3306
default-character-set=gbk www.shengfang.org
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=gbk www.shengfang.org
引用 cnchun
perl mysql中文亂碼
摘自 中國Perl協會 用戶推廣組 cnchun 2005-06-29 16:57
http://perlchina.sun126.com/cgi-bin/ccb/index.cgi
搞了一天總算搞好了。
perl5.8+mysql4.1,用perl DBI mysql往數據庫插入數據,總是亂碼,而用phpmyadmin能插入中文;而用perl插入的中文在phpmyadmin裡面也是亂碼。數據庫的創建,我用了。
create database cnews default character set gbk;
CREATE TABLE `news` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(50) default NULL,
`context` text,
`date` date default NULL,
`site` varchar(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk
如果在my.cnf裡面設置了
[client]
default-character-set = gbk
[mysqld]
default-character-set = gbk
mysql -uroot -p;
mysql>use cnews;
mysql> status
--------------
Server characterset: gbk
Db characterset: gbk
Client characterset: gbk
Conn. characterset: gbk
則perl能夠正常插入中文無須其它設置。
如果不在my.cnf裡設置default-character-set,例如:
mysql -uroot -p;
msyql>use cnews
mysql> status
Server characterset: latin1
Db characterset: gbk
Client characterset: latin1
Conn. characterset: latin1
在調用perl 的時候,需要下面設置:
$db->do("SET character_set_client = 'gbk'");
$db->do("SET character_set_connection = 'gbk'");
如果需要顯示結果,加上
$db->do("SET character_set_results= 'gbk' ");
其實可以把database 設置為gbk,然後用character_set_*來設置相關編碼,mysql其實還是很靈活的。perl運行跟linux的locale沒有關係,對於數據庫操作。
剛學perl,希望對跟我一樣的新手有所幫助。
hoowa
在Perl裡簡單的執行這個也.
摘自 中國Perl協會 用戶推廣組 hoowa 2005-06-29 19:00
http://perlchina.sun126.com/cgi-bin/ccb/index.cgi
在Perl裡簡單的執行這個也可以實現與服務器端一樣是gbk編碼
set names gbk
使用connect方法建立一個到數據源的連接。$data_source應該以DBI:driver_name:開始,例如:
$dbh = DBI->connect("DBI:mysql:$database", $user, $password); www.shengfang.org
$dbh = DBI->connect("DBI:mysql:$database:$hostname",$user, $password);
$dbh = DBI->connect("DBI:mysql:$database:$hostname:$port",$user, $password);
Can't locate DBI.pm in at INC (@INC contains: O:/php/perl/lib O:/php/perl/site/lib dot ) at O:\php\OptiPerl\webroot\cgi-bin\newscript1.cgi line 3.
BEGIN failed--compilation aborted at O:\php\OptiPerl\webroot\cgi-bin\newscript1.cgi line 3.
#!O:/php/perl/bin/Perl.exe www.shengfang.org
print "Content-type: text/plain; charset=gb2312\n\n";
print "rrow[1]\n";
use DBI; www.shengfang.org
my $dbh = DBI->connect('dbi:mysql:astbill:localhost','root','123');
$dbh->do("set names gbk");
my $sql = "insert into admin(namee,pwd) values('對數函數','閃電劃過的')";
my $sth = $dbh->prepare($sql);
$sth->execute(); www.shengfang.org
my $sql = "select * from admin";
my $sth = $dbh->prepare($sql);
$sth->execute(); www.shengfang.org
my at row;
while(@row=$sth->fetchrow_array())
{ www.shengfang.org
print "$row[2]\n"; www.shengfang.org
}
rrow[1]
21232f297a57a5a743894a0e4a801fc3
?????
rrow[1] www.shengfang.org
21232f297a57a5a743894a0e4a801fc3
閃電劃過的 www.shengfang.org
[client]
port=3306
default-character-set=gbk www.shengfang.org
# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=gbk www.shengfang.org
引用 cnchun
perl mysql中文亂碼
摘自 中國Perl協會 用戶推廣組 cnchun 2005-06-29 16:57
http://perlchina.sun126.com/cgi-bin/ccb/index.cgi
搞了一天總算搞好了。
perl5.8+mysql4.1,用perl DBI mysql往數據庫插入數據,總是亂碼,而用phpmyadmin能插入中文;而用perl插入的中文在phpmyadmin裡面也是亂碼。數據庫的創建,我用了。
create database cnews default character set gbk;
CREATE TABLE `news` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(50) default NULL,
`context` text,
`date` date default NULL,
`site` varchar(20) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gbk
如果在my.cnf裡面設置了
[client]
default-character-set = gbk
[mysqld]
default-character-set = gbk
mysql -uroot -p;
mysql>use cnews;
mysql> status
--------------
Server characterset: gbk
Db characterset: gbk
Client characterset: gbk
Conn. characterset: gbk
則perl能夠正常插入中文無須其它設置。
如果不在my.cnf裡設置default-character-set,例如:
mysql -uroot -p;
msyql>use cnews
mysql> status
Server characterset: latin1
Db characterset: gbk
Client characterset: latin1
Conn. characterset: latin1
在調用perl 的時候,需要下面設置:
$db->do("SET character_set_client = 'gbk'");
$db->do("SET character_set_connection = 'gbk'");
如果需要顯示結果,加上
$db->do("SET character_set_results= 'gbk' ");
其實可以把database 設置為gbk,然後用character_set_*來設置相關編碼,mysql其實還是很靈活的。perl運行跟linux的locale沒有關係,對於數據庫操作。
剛學perl,希望對跟我一樣的新手有所幫助。
hoowa
在Perl裡簡單的執行這個也.
摘自 中國Perl協會 用戶推廣組 hoowa 2005-06-29 19:00
http://perlchina.sun126.com/cgi-bin/ccb/index.cgi
在Perl裡簡單的執行這個也可以實現與服務器端一樣是gbk編碼
set names gbk
<< Home