Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1prOs0-0000si-DR for pgsql-general@arkaria.postgresql.org; Tue, 25 Apr 2023 20:02:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1prOrz-00053n-Bn for pgsql-general@arkaria.postgresql.org; Tue, 25 Apr 2023 20:02:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1prOrz-00053c-0z for pgsql-general@lists.postgresql.org; Tue, 25 Apr 2023 20:02:35 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1prOru-001rvL-NQ for pgsql-general@lists.postgresql.org; Tue, 25 Apr 2023 20:02:33 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id D74C445D27; Tue, 25 Apr 2023 22:02:24 +0200 (CEST) Date: Tue, 25 Apr 2023 22:02:24 +0200 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: DBD::Pg (version 3.16.3) returns EMPTY char columns as 'undef' Message-ID: <20230425200224.vccz67t4t72iw3wi@hjp.at> Mail-Followup-To: pgsql-general@lists.postgresql.org References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ffw6wmfuapr2dlvd" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --ffw6wmfuapr2dlvd Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2023-04-25 14:41:45 +0200, Matthias Apitz wrote: > We're using the above DBD::Pg version on Linux together with PostgreSQL 1= 5.1 > On fetch empty char columns are returned as (Perl) 'undef' >=20 > while ( my @row_ary =3D $dba->FetchArray()) { What is FetchArray? Neither perldoc DBI nor perldoc DBD::Pg mentions this method. Did you use a wrapper around DBI? (I would have expected fetchrow_array here) > foreach my $i (0..$#row_ary) { > if ($row_ary[$i] eq undef) { > print $row_ary[1] . "\n"; > next; So when any column is null you want to print the first one and skip to the next one? > } > ... > which later leads in our code to NULL values '\N' in the writing of a CSV= -like export > files. Ofc NULL values in the database are something else as '' char > strings.=20 Works for me (PostgreSQL 14, Perl 5.34, DBI 1.643, DBD::Pg 3.15): % cat empty_char #!/usr/bin/perl use v5.34; use warnings; use Data::Dumper; use DBIx::SimpleConnect; my $dbh =3D DBIx::SimpleConnect->connect("default"); $dbh->do("drop table if exists empty_char"); $dbh->do("create table empty_char (id serial primary key, t char(5))"); $dbh->do("insert into empty_char(t) values(null)"); $dbh->do("insert into empty_char(t) values('')"); $dbh->do("insert into empty_char(t) values(' ')"); $dbh->do("insert into empty_char(t) values('a')"); $dbh->do("insert into empty_char(t) values('a ')"); my $data =3D $dbh->selectall_arrayref( "select * from empty_char", {Slice =3D> {}} ); print Dumper($data); (DBIx::SimpleConnect is just a simple wrapper which looks up connection strings. It returns a normal DBI database handle object) % ./empty_char $VAR1 =3D [ { 't' =3D> undef, 'id' =3D> 1 }, { 'id' =3D> 2, 't' =3D> ' ' }, { 't' =3D> ' ', 'id' =3D> 3 }, { 't' =3D> 'a ', 'id' =3D> 4 }, { 'id' =3D> 5, 't' =3D> 'a ' } ]; hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --ffw6wmfuapr2dlvd Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmRIMcwACgkQ8g5IURL+ KF37/g//SdyCPmckrGpFualSpkNA+fGFcifoK1f4WfTUpgg41yw3r7PeUgHVnrw1 IGWlXNUQVfB8Ahxr2ukoQcIgQNm/QY3fqkZdIcrA+xdhT+IRfLAYU8lIoWjnfrax /ndB4CJrLEkmBxq1w4ojnXuWnPJsAsPa5HPK9272D/IHpQ/R+YbLwVyY7VJ6tpiS noswq5r05dlFxVmfZoIRS2n4QVg2BgczTmqB0Phlvv5fbvtCrmkuk1h3p4miOiOa ExVgCAhs71fCfJYq8BXL1a2ZOIBPDtr4610sXkGTuGD+Xalg9Kg8KGER5waHeTGO rg0zwHwJ0ZrNvIYqqdU67BMT65URypqn5ql0cl5aAUQphn1FUJl1YzV0qc0wkVhu qpuUy6J6c0IjDInm6gqMKPjogxNCD3xRPZtp2sDsHbamcmZ0mfdGGkUSdQHoVGT+ IbKdGRwzt/5hsYd9VDUDm8jN6WxduxqlY7+DMV4YF4N1q8mqm5hQelSmkMB+48Zl /nue+HcDDK9Us+56og64k0rDzwpErPw8Hlo47Kljlw82oIELWpHo37ZzPzG8N6KZ IPz2cciUgajBBonOcTofIu2aEEbuLomtWL0wsWeFotOocHRWwcuZcPX2v+ZSz054 OcZpYQYWJBnIHQMVtdKRmg9QTmxWkFqlgw00/Kvdu57O7QD2KVk= =oHfy -----END PGP SIGNATURE----- --ffw6wmfuapr2dlvd--