• 締切済み

文字セットの変換

perlCGIでperlDBIからPostgreSQLに接続してデータを文字セットをUTF-8に変換して表示するプログラムを作成していますが、この度、新サーバに移設したところ文字変換がうまくいかないようです。 これまでは、文字セット変換は、 $ENV{'PGCLIENTENCODING'} = 'UTF-8'; でうまく作動しておりましたが、新サーバではだめなようです。 そのため $dbh = DBI->connect("DBI:Pg:dbname=$dbname", "$dbusr", "$dbpas") || die &error(not_connect); $sth = $dbh->prepare( "SET client_encoding TO 'UTF-8' ") || die &error(not_prepare); $sth->execute() || die &error(not_execute); $sth->finish || die &error(not_finish); で変換を試してみましたがこれもだめでした。 なにが原因なのか分かりません。 ご回答をよろしくお願いします。 新サーバの環境は、 OS:Debian sarge PostgreSQL7.4.7 DBI 1-46 DBD 1-41 perl 5.8 です。

みんなの回答

  • Ethersky
  • ベストアンサー率71% (168/235)
回答No.1

UTF-8ではなくUNICODEでどうですか? あと、結果を得るわけではないので、prepare→execute→finishではなくdoでもいいでしょう。 $dbh->do("SET NAMES 'UNICODE'");

関連するQ&A

  • Mysqlの接続について

    現在、以下のようなperlのスクリプトを作っているのですが、mysqlへの接続,切断は以下の場合、どちらにした方がいいのでしょうか? (1)の場合 use DBI; $dsn="DBI:mysql:database=dbname:host=localhost"; $dbh=DBI->connect($dsn,'user','pass'); $sth = $dbh->prepare("SELECT no,title,name,date,host From `table1` where no='1'"); $sth->execute; $sth->fetchrow_array; $sth->finish; $sth2 = $dbh->prepare("SELECT no,title,name,date,host From `table2` where no='1'"); $sth2->execute; $sth2->fetchrow_array; $sth2->finish; $dbh->disconnect; (2)の場合 use DBI; $dsn="DBI:mysql:database=dbname:host=localhost"; $dbh=DBI->connect($dsn,'user','pass'); $sth = $dbh->prepare("SELECT no,title,name,date,host From `table1` where no='1'"); $sth->execute; $sth->fetchrow_array; $sth->finish; $dbh->disconnect; $dsn="DBI:mysql:database=dbname:host=localhost"; $dbh=DBI->connect($dsn,'user','pass'); $sth2 = $dbh->prepare("SELECT no,title,name,date,host From `table2` where no='1'"); $sth2->execute; $sth2->fetchrow_array; $sth2->finish; $dbh->disconnect; (1)の場合と(2)の場合の違いは、(1)の場合、最初にデータベースに接続して、一番最後に切断する事で、(2)の場合、毎回データベースへの接続と切断を行う事が違います。 (2)の場合、毎回接続と切断を行うので、2回ほどではあまり変わらないかもしれないのですが、もし10回とか接続と切断を行うとかなり負荷が高くなるような気がするのですが、他のHPなどを拝見すると接続をしたら切断を行うように癖をつけるようにした方がいいと書かれていたりします。 どちらの方が正しいやり方というか、負荷がすくなく使えるのでしょうか?

    • ベストアンサー
    • Perl
  • MYSQL 4.1 の余計なエスケープ処理

    おはようございます。PERL 5.8.5 からMYSQL4.1 サーバーへのデータINSERT 時の文字変換処理について教えてください。 MYSQL 4.1 の文字コードは次の通りです。 SHOW VARIABLES LIKE 'character?_set?_%'; の結果 character_set_client :binary character_set_connection:binary character_set_database:utf8 character_set_results:binary character_set_server:utf8 character_set_system:utf8 MYSQL 4.1 サーバーが上記のような状態で、PERL で次の処理をします。なお、PERL ソース自体はEUC で書かれています。 #!/usr/bin/perl use Jcode; use DBI; $string = "あああ"; Jcode::convert( ?$string, 'utf8', 'euc' ); $dbh = DBI->connect( 'DBI:mysql:DB名:ホスト名:3306', 'ユーザー名', 'パスワード' ,{AutoCommit => 0} ); $sth = $dbh->prepare( "set names utf8" ); $sth->execute; $sth->finish; $sth = $dbh->prepare( "insert into test values( ? )" ); $sth->bind_param( 1, $string ); $sth->execute; $sth->finish; $dbh->commit; $dbh->disconnect; 上記のソースを実行すると、MYSQL 4.1 サーバーには [あ?あ?あ] のような、余計なエスケープが掛かった状態で文字がINSERT されているのです。 私としては、set names utf8 で、MYSQL 4.1 サーバーとクライアントで同様の文字コードを使うようにしているので、MYSQL 4.1 における余計な文字コード変換はないものと思っています。余計な文字コード変換がないにもかかわらずエスケープ文字が入る理由が分かりません。 この現象についてお分かりの方がいらっしゃいましたら教えて頂きたく思います。なお、ネットでも調べたのですが、どの方も未解決でしたので質問さえて頂きました。 お願いします。

  • データベースが返すエラー文字列について

    OSのバージョンUPに伴いoracle、perl、DBI等のバージョンもあげました perl 5.8.7 DBI-1.52 oracle9i perl 5.005_03 DBI-1.13 oracle8.0.5 その関係かはよくわからないのですが、エラー処理の実行結果が異なってしましました $sth = $dbh->prepare("$sql") or die "Cannot prepare: " . $dbh->errstr(); $sth->execute() or die print "データベースエラー"; このSQL文の実行結果はエラーの際、「データベースエラー」というdieメッセージとなります しかしバージョンをあげた方では、メッセージが表示されません $sthを出力してみたところ、空でした エラーでない場合の実行結果は同じになります 原因がわからず困っています 同じようにエラー処理(エラーメッセージを表示させたい)のですが、どのようにしたらよいでしょうか? どなたかご教授ください 宜しく御願いします

    • ベストアンサー
    • Perl
  • PerlでDBIを使いますが、結果セットをフィールド名でアクセスするには?

    OSはWindowsでAccess2002のデータベースをODBC経由でDBIを用いて、Perlからアクセスします。 以下のように、結果セットをフィールド名でアクセスしたいです。 可能でしょうか? #!/perl/bin/perl #!/usr/bin/perl -w use DBI; # create the DSN connection $dsn = "dbi:odbc:test"; $dbh = DBI->connect($dsn) or die "接続エラー"; $query = "select * from goods;"; # execute the query $sth = $dbh->prepare($query); $sth->execute(); print "no name price\n"; print "--------------------------------------\n"; while(@item = $sth-> fetchrow) { #print "@item[0] @item[1] @item[2]"; #----- 通常はこうだが、 print "$item{'goods_name'} $item{'goods_price'} $item{'auto_no'}"; #----こんな感じで利用したい。 print ("\n"); } # disconnect $sth->finish(); $dbh->disconnect(); exit; __END__

    • ベストアンサー
    • CGI
  • DBIモジュールと Perl5.8

    Perl5.8のエンコードが理解できずに困っております。 MySQLでは、utf8の文字コードのデータがあります。 以下のスクリプトを実行させると、 Wide character in print at C:/usr/local/site/lib/DBI.pm line 1008. となります。 対処方法はあるのでしょうか? use utf8; use strict; use DBI; my $dbh = DBI->connect("dbi:mysql:dbname=test", "root", "", { AutoCommit=>1, PrintError=>1, RaiseError=>1} ); my $fname = "test.txt"; my $sth = $dbh->prepare("select id, namae from test order by id asc;"); $sth->execute(); # 出力ファイルを開く。 open FILE, ">" , $fname or die "Can't open $fname: $!"; # 整形された結果をファイルにダンプする。 my $rows = $sth->dump_results(80, "\n", ",", \*FILE); # 出力ファイルを閉じる。 close FILE or die "Error closing result file: $!\n"; $dbh->disconnect();

    • ベストアンサー
    • Perl
  • Perlでの文字コード変換

    Windowsのフォルダに入っているファイル名をFedora Coreに入ってるpostgresのtableに入れたいのですが(perlで実行)文字の変換の仕方がわからず以下のメッセージが出て止まってしまいます。 DBD::PgPP::st execute failed: ERROR: invalid byte sequence for encoding "EUC_JP": 0x96d8 良い方法があれば教えてください。 **************************************** $query0 = 'insert into aaatbl (filename) values("$aaa"); $sth = $dbh->prepare("$query0"); $sth->execute; $sth->finish; ($query0 が S-JISでaaatblはECU-JP)

    • ベストアンサー
    • Perl
  • perlを使ってレコード件数を取り出してみると、すごい数が・・・。

    perlのDBIモジュールを使って、mysqlのレコード件数を表示させてみると、すごい数が表示されました。ソースは以下の通りです。 use DBI; $dbh=DBI->connect("DBI:mysql:データベース名:サーバ名", "ユーザ名", "パスワード") || die $DBI::errstr; if(!$dbh){ print "エラーです。入力を確認してください。"; exit; }else{ #接続成功 # SELECT文発行 $sth=$dbh->prepare("SELECT sei, mei, mail FROM table"); #実行 $sth->execute; $num_rows = $sth->rows; print "該当 $num_rows, 件\n"; # 開放 $sth->finish; # AUTO COMMITがOFFの場合はCOMMITが必要 $dbh->commit; # 切断 $dbh->disconnect; } 実行すると該当 4294967294, 件 とブラウザに表示されました。僕はレンタルサーバを使っているわけですが、僕のデータベースにはレコードは3件しかないのです。これはレンタルサーバのデータベースすべての件数が表示されているのでしょうか? なんだかSQLを実行するのが怖いので、考えられる原因を教えてください。よろしくお願いします。 プログラムを初めてまだ、数ヶ月しか経ってない初心者なので、みなさんの知恵を貸していただけると幸いです。

    • ベストアンサー
    • MySQL
  • fetchrow_arrayとfetchrow_hashrefの使い方

    Jやまとです。 PerlからSELECT文を実行して結果の行の値を取得する単純なスクリプトを作成しています。 (1)fetchrow_array使用 (2)fetchrow_hashref使用 以上2通りの方法で試したのですが、(2)がInternal Server Errorになります。 原因が全く分かりません。 分かる方いらしゃいましたら、ご教授願います。 OS:TurboLinux6.2 DB:Oracle8i Apache,PerlはTurboLinux6.2に入っているものをそのまま使用 以下、ソースを記載します。 --------------- (1)fetchrow_arrayを使用して行の値を取得(こっちは動く) $dbh = DBI->connect($ds, $user, $pass) || &dbErr("Database can't connect." . $DBI::errstr); $sql = "SELECT SYSDATE FROM DUAL \n"; $sth = $dbh->prepare( $sql ); $sth->execute or die "Cannot execute. " . $sth->errstr(); $cnt1 =1; while (@row = $sth->fetchrow_array()) { @{$get_date[$cnt1]} = @row; $cnt1++; } --------------- (2)fetchrow_hashrefを使用して行の値を取得(こっちが動かない) $dbh = DBI->connect($ds, $user, $pass) || &dbErr("Database can't connect." . $DBI::errstr); $query = qq{ SELECT SYSDATE FROM DUAL }; $sth = dbh->prepare($query); $sth->execute(); # 実行 if($sth->rows() != 1){ # 該当する行数 # エラー処理 } $rhash = $sth->fetchrow_hashref(); %hash = {%{$rhash}}; $sth->finish(); $get_date = $hash{SYSDATE}; --------------- (2)実行時のerror_log Can't locate object method "prepare" via package "dbh" at /u01/ora1/www/htdocs/hoge.cgi line 30. Premature end of script headers: /u01/ora1/www/htdocs/hoge.cgi --------------- (本文長くてすみません)

    • ベストアンサー
    • Perl
  • perlのプログラムでエラーが出ます。

    普段PHPerなんですがbatchの関係でperlを使用しなければならなくなり書いていたんですが、 下記のソースコードの中でINSERT INTOの部分でエラーが発生してしまいます。 最初selectしている部分はcreate tableで作っているテーブル構成と同じです。 それからサイト名に関するレコード部分は*で隠しています。 よろしくお願いいたします。 #!/usr/bin/perl use DBI; # データソース $d = 'DBI:mysql:a_anime'; # ユーザ名 $u = '************'; # パスワード $p = '************'; # データベースへ接続 $dbh = DBI->connect($d, $u, $p); $sth = $dbh->prepare("select * from tweet where tweetid BETWEEN '1' AND '21562745' limit 5"); $sth->execute; while( @row = $sth->fetchrow_array ){ $priduct_id = @row['3']; $user_url = @row['6']; $sth2 = $dbh->prepare("show tables from a_anime like 'tweet_@row['3']'"); $sth2->execute; @row2 = $sth2->rows; $sth2->finish; if (@row2 != 1){ $sth22 = $dbh->prepare("CREATE TABLE `tweet_@row['3']` ( `id` int(255) NOT NULL auto_increment, `userid` int(25) DEFAULT NULL, `text` text NOT NULL, `product_id` int(255) NOT NULL, `product_name` varchar(30) NOT NULL, `date` varchar(20) NOT NULL, `user_url` varchar(160) NOT NULL, `username` varchar(100) NOT NULL, `flag` int(1) NOT NULL DEFAULT '1', `rev_point` int(1) NOT NULL DEFAULT '1', `t_tweetid` bigint(255) DEFAULT NULL, `rev_id` int(10) DEFAULT NULL, `rev_flag` int(1) NOT NULL DEFAULT '0', `*****_flag` int(1) DEFAULT NULL, `res_info` int(100) DEFAULT NULL, `fav_info` int(255) DEFAULT NULL, `user_img` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`), KEY `index3` (`flag`), KEY `user url` (`user_url`), KEY `t_tweetid` (`t_tweetid`), KEY `index4` (`flag`,`rev_point`,`product_id`), KEY `index5` (`flag`,`product_id`), KEY `index6` (`userid`,`flag`), KEY `index7` (`username`,`flag`), FULLTEXT KEY `index2` (`text`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8"); $sth22->execute; $sth22->finish; } $sth3 = $dbh->prepare("select user_url from tweet_@row['3']"); $sth3->execute; print "@row['3']\n"; print "@row['6']\n"; @row3 = $sth3->fetchrow_array; $sth3->finish; #if ($user_url != $row3){ print @row['2'] ; print "\n"; $sth4 = $dbh->prepare("INSERT INTO tweet_@row['3'] VALUES (@row['1'],@row['2'],@row['3'],@row['4'],@row['5'],@row['6'],@row['7'],@row['8'],@row['9'],@row['10'],@row['11'],@row['12'],@row['13'],@row['14'],@row['15'],@row['16'])"); print $sth4; print "\n"; $sth4->execute; $sth4->finish; } } $sth->finish; $dbh->disconnect;

  • 未だpostgresにアクセス出来ません。教えて下さい。

    こんにちわ。windowsXP,postgres8.2,perl5を使用して居ります。 postgresにperlからアクセスし、既存のテーブルの行に値を入れたいのですが、 valuesに数字以外の文字を指定するとpostgresに文字列が認識されません。教えて頂きたいことは、 仮に数字を6行目の$に入れると認識されるのですが、文字だと出来ないのは、どうしてでしょうか。$id='○○'と 文字を入れてみるとpostgresには1の結果だけが入ってきました。どのようにしたら良いかと、 思いprint'○○',$idとしてみました。すると下記のプロンプトでのエラーがでました。 構文が間違えていると思うので、良い方法があったら教えて下さい。宜しくお願いします。 #!c:/perl/bin/perl use DBD::Pg; use DBI; require'jcode.pl'; $dbh=DBI->connect("dbi:Pg:host=localhost;dbname=kensaku",'windws','aaaa')||die $dbh->errstr; print'9',$id;print'山田',$word1;print'd',$word2;print'w',$word3;print'c',$head;print'a',$img; @bindvalues=($id,$word1,$word2,$word3,$head,$img); &jcode::convert(*bindvalues,'euc'); my$b=(<<"EOL"); insert into kensaku(id,word1,word2,word3,head,img) values($id,$word1,$word2,$word3,$head,$img) EOL ; $sth=$dbh->prepare($b)||die $dbh->errstr; $sth->execute()||die $sth->errstr; $dbh->disconnect; $sth=$dbh->prepare($b)||die $dbh->errstr; $sth->execute()||die $sth->errstr; $dbh->disconnect; コマンドプロンプトのエラー DBD::Pg::st execute failed: ERROR: syntax error at or near "," at character 103 at test.pl line 19. ERROR: syntax error at or near "," at character 103 at test.pl line 19.