0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MariaDB に perl で接続した時の文字化けの状況

Last updated at Posted at 2020-06-21

Perl の DBI で MariaDB に接続した時、文字化けするケースと文字化けしないケースがあるので調べてみました。

テストに使ったプログラム

maria_read_check.pl
# ! /usr/bin/perl
#
#	maria_read_check.pl
#
#					Jun/21/2020
#
# -----------------------------------------------------------------------
use	strict;
use	warnings;
use	utf8;
use	Encode;
use	DBI;
#
# -----------------------------------------------------------------------
sub mysql_version_proc
{
	my $db = $_[0];
	my $sth = $db->prepare("SELECT VERSION()");
	$sth->execute;
	my $num_rows = $sth->rows;

	for (my $it=0; $it<$num_rows; $it++)
		{
		my @a = $sth->fetchrow_array;
		print @a;
		}
	print "\n";

	$sth->finish;
}

# -----------------------------------------------------------------------
sub sql_show_proc
{
	my $db = $_[0];

	print "*** sql_show_proc ***\n";

	my $sth = $db->prepare("SELECT * FROM cities");
	$sth->execute;
	my $num_rows = $sth->rows;

	for (my $it=0; $it<$num_rows; $it++)
		{
		my @a = $sth->fetchrow_array;
		print "$a[0]\t$a[1]\t$a[2]\t$a[3] \n";
		}

	$sth->finish;
}
# -----------------------------------------------------------------------
print STDERR (encode ('utf-8',"*** 開始 ***\n"));
#
my $constr='DBI:mysql:city:localhost';
my $user = 'scott';
my $passwd = 'tiger123';
#
my $dbi=DBI->connect($constr, $user, $passwd);
mysql_version_proc($dbi);
sql_show_proc($dbi);
#
$dbi->disconnect;
#
print STDERR (encode ('utf-8',"*** 終了 ***\n"));
# -----------------------------------------------------------------------

Arch Linux での実行結果

$ ./maria_read_check.pl 
*** 開始 ***
10.4.13-MariaDB
*** sql_show_proc ***
t3321	岡山	915842	2005-6-24 
t3322	倉敷	364278	2005-8-8 
t3323	津山	758641	2005-9-17 
t3324	玉野	516432	2005-7-22 
t3325	笠岡	327159	2005-2-28 
t3326	井原	792581	2005-5-9 
t3327	総社	635197	2005-4-10 
t3328	高梁	485136	2005-10-8 
t3329	新見	164827	2005-5-21 
*** 終了 ***

Ubuntu 20.04

$ ./maria_read_check.pl 
*** 開始 ***
10.3.22-MariaDB-1ubuntu1
*** sql_show_proc ***
t3321	岡山	315842	2005-6-24 
t3322	倉敷	964278	2005-8-8 
t3323	津山	758641	2005-9-17 
t3324	玉野	392145900	2020-6-21 
t3325	笠岡	327159	2005-2-28 
t3326	井原	792581	2005-5-9 
t3328	高梁	485136	2005-10-8 
t3329	新見	164827	2005-5-21 
*** 終了 ***

Ubuntu 19.10

$ ./maria_read_check.pl 
*** 開始 ***
8.0.20-0ubuntu0.19.10.1
*** sql_show_proc ***
t3321	岡山	691734	2001-02-15 
t3322	倉敷	923657	2001-08-01 
t3323	津山	279358	2001-07-29 
t3324	玉野	284615	2001-05-18 
t3325	笠岡	741256	2001-01-17 
t3326	井原	497521	2001-04-21 
t3327	総社	217348	2001-07-15 
t3328	高梁	152978	2001-09-11 
t3329	新見	823495	2001-10-02 
*** 終了 ***

Ubuntu 18.04

$ ./maria_read_check.pl 
*** 開始 ***
5.7.30-0ubuntu0.18.04.1
*** sql_show_proc ***
t3321	??	975842	2005-6-24 
t3322	??	314278	2005-8-8 
t3323	??	758641	2005-9-17 
t3324	??	392145900	2020-6-21 
t3325	??	327159	2005-2-28 
t3326	??	792581	2005-5-9 
t3328	??	485136	2005-10-8 
t3329	??	164827	2005-5-21 
*** 終了 ***

RaspberryPi

$ ./maria_read_check.pl 
*** 開始 ***
10.3.22-MariaDB-0+deb10u1
*** sql_show_proc ***
t3321	??	175842	2005-6-24 
t3322	??	314278	2005-8-8 
t3323	??	758641	2005-9-17 
t3324	??	516432	2005-7-22 
t3325	??	327159	2005-2-28 
t3326	??	792581	2005-5-9 
t3327	??	635197	2005-4-10 
t3328	??	485136	2005-10-8 
t3329	??	164827	2005-5-21 
*** 終了 ***

関連ページ
MariaDB に perl で接続した時の文字化け対策

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?