Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZ8Ft-0003ZI-RV for pgsql-general@arkaria.postgresql.org; Tue, 18 Dec 2018 05:49:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gZ8Fs-00028S-Cm for pgsql-general@arkaria.postgresql.org; Tue, 18 Dec 2018 05:49:20 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gZ8Fr-00028L-9B for pgsql-general@lists.postgresql.org; Tue, 18 Dec 2018 05:49:20 +0000 Received: from nh502-vm9.bullet.mail.kks.yahoo.co.jp ([183.79.56.154]) by makus.postgresql.org with smtp (Exim 4.89) (envelope-from ) id 1gZ8Ff-000201-FH for pgsql-general@lists.postgresql.org; Tue, 18 Dec 2018 05:49:14 +0000 Received: from [183.79.100.140] by nh502.bullet.mail.kks.yahoo.co.jp with NNFMP; 18 Dec 2018 05:49:02 -0000 Received: from [183.79.100.133] by t503.bullet.mail.kks.yahoo.co.jp with NNFMP; 18 Dec 2018 05:49:02 -0000 Received: from [127.0.0.1] by omp502.mail.kks.yahoo.co.jp with NNFMP; 18 Dec 2018 05:49:02 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 662887.43229.bm@omp502.mail.kks.yahoo.co.jp X-YMail-OSG: Su2MxpgVM1naigECI.LxnABqmL.WehXBTasmTSp0rRwg7Zfo1b7gL9EHEuByniL hG99vgLt9.PyVmlrAsEXMCb088DQM7C1kfsD9FMBsU3utRU5EMj34J9GIf9fZygZldaSHstFK.xP j2YF4pqW.fehLCTgEaQUCDbiGXksIdIQLFosEb9dI_gmiTRRBinunH92Xd6oMFCVGygj7YtTASCQ qTGLe0NKe12eBCeY0GS9hdEppXQXdJOlr2DupjLuQ9sUpci2g_Z6HUkqzcmIT5WiTzWT.m1xA8r3 w0HXMZgBom6Cr2gcpK_x39aaMWGEVXqTIad0hJDxB0azzKLd9Kyw.Ut0kbKmvxuwgaqbwTVm7R0Y O1YqXJz4Dni_Y_b8XkDA7XIIn6WDUqhNlHD4ru4jqc_uiLjvwjFbupgT2TSwjue0AO.J7b0mREt_ 6vysIxW8dq0WzGZ5SS3k4PPxLBk01q8lZVod7uB73ZqAkm7LEDsXnGpBaPVP4gE5ZcvE_f0lC3x5 ssWtB5cqtnjhz0ZUp4Di_uv5xufubSNTz8DR_BFUk83NjT28TYvTDoBgZLKV4e4hCwvjX.Q48hUe 09hyF7QGfNXsBf3coDtVLbwi8oIV82Obd2RuKbFI- Received: from jws700001.mail.kks.yahoo.co.jp by sendmailws502.mail.kks.yahoo.co.jp; Tue, 18 Dec 2018 14:49:02 +0000; 1545112142.185 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1545112142; s=yj20110701; d=yahoo.co.jp; h=Date:From:Reply-To:To:Message-ID:In-Reply-To:References:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding; bh=ri+XxsW9fkzGuWsSsNc0p6kjgmest4UXHDaJtlw2fA8=; b=nPmcxRugHzoDq8+l4DPtdQDz8LT+TAYl5W1GGwktpmHYWHevUUjkTlUDvoCXn8OT cqmbPn17gHPRkrGq8CVdBKGYMFx7/25JuLRnaUbebrxMaxwRaiC02SHsZNTYJRGs7hQ 3jIPkZFdXDotLYNk+VJRBk81tRPanW8DQoyyLSD0= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=yj20110701; d=yahoo.co.jp; h=Date:From:Reply-To:Message-ID:In-Reply-To:References:MIME-Version:Content-Type:Content-Transfer-Encoding; b=kqcIhQyVNwjFnwpoKSZ4P2TbYV100ClLa65E5d+enWP5JRTGsZ9rPeKQ1vW2zNH0 UjmBGWp/Mzmveb2o9jFY8qVJlirYh67QeVerTjRr1ThD1NV09OUclNbD0L/cfA0+Jdz +uTHOIb9iXwPOq5vXoJPVQJnNmE2pz2ma8z04vrU=; Date: Tue, 18 Dec 2018 14:49:01 +0900 (JST) From: Reply-To: To: rob stone , "pgsql-general@lists.postgresql.org" Message-ID: <2012722956.616241.1545112141488.JavaMail.yahoo@mail.yahoo.co.jp> In-Reply-To: References: <904538479.543179.1545050043907.JavaMail.yahoo.ref@mail.yahoo.co.jp> <904538479.543179.1545050043907.JavaMail.yahoo@mail.yahoo.co.jp> Subject: Re: Creating 2D arrays for pg_copy_from, reading tab-delimited text file that contains comma and double quotes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Length: 6248 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello Rob, Thank you for your comments. As I wrote just a few minutes ago, using a tab delimiter inside the implode= as suggested by Daniel (and specifying the read length ("0") for the fgetc= sv has solved this problem. At one point, I had tried to use the last parameter - enclosure character -= in your reply, but I got an error, something like, Warning: pg_copy_from(): Copy command failed: ERROR: missing data for colum= n=20 "item1" CONTEXT: COPY test_table, line 1: "1 Denny's orange juice 1,500=20 yen" ... Then Daniel's reply came and I tried that, still made errors here and there= =20 (removed the read length- but forgot to put it back in subsequent=20 tests), and was looking for ways to strip off the leading and trailing=20 double quotes while leaving the comma intact. FF now.. Anyway, crisis averted this time! Thank you again for taking time to share your thoughts. Cheers! ----- Original Message ----- > From: rob stone > To: s400t@yahoo.co.jp; "pgsql-general@lists.postgresql.org" > Cc:=20 > Date: 2018/12/17, Mon 23:38 > Subject: Re: Creating 2D arrays for pg_copy_from, reading tab-delimted te= xt file that contains comma and double quotes >=20 > Hello, >=20 > On Mon, 2018-12-17 at 21:34 +0900, s400t@yahoo.co.jp wrote: >> Hello Good People of the Forum! >>=20 >> I am trying to insert some data into a PostgreSQL database using PHP >> and struggling to create an array so that pg_copy_from function will >> accept and process the data. >>=20 >> I can insert data but not the way I want- my data this case contains >> comma, space, double quotes and unpaired double quote. >> I need to use Excel to create the data and save it as tab delimited >> text file, and then convert encoding to UTF-8 (from Excel's ANSI) >> before passing it to the PHP.=20 >>=20 >> Here is my dummy data in Excel: >> rec_no=C2=A0 =C2=A0 item1=C2=A0 =C2=A0 item2=C2=A0 =C2=A0 item3=C2=A0 = =C2=A0 item4=C2=A0 =C2=A0 item5 >> 1=C2=A0 =C2=A0 Denny's=C2=A0 =C2=A0 orange juice=C2=A0 =C2=A0 1,500 yen= =C2=A0 =C2=A0 "Dear John"=C2=A0 =C2=A0=20 > 32" TV >>=20 >> Explanation: the first row is header. >> The second row is data for each column. >> I tried to create variation like apostrophe, space between words, >> comma, double quotes and unpaired double quote (32" TV). >>=20 >> When I save that Excel as a tab delimited text file, I get this: >> rec_no=C2=A0 =C2=A0 item1=C2=A0 =C2=A0 item2=C2=A0 =C2=A0 item3=C2=A0 = =C2=A0 item4=C2=A0 =C2=A0 item5 >> 1=C2=A0 =C2=A0 Denny's=C2=A0 =C2=A0 orange juice=C2=A0 =C2=A0 "1,500 ye= n"=C2=A0 =C2=A0=20 > """Dear John"""=C2=A0=20 >> "32"" TV" >> (As seen when I opened that file with Notepad) >>=20 >> Because my data also contains non-ascii, I saved the file with UTF-8 >> encoding using the Notepad. >>=20 >> Then I created a two-dimensional array with PHP: >>=20 >> 1=EF=BC=8E$file =3D 'test.txt'; //tab delimited file=20 >> 2=EF=BC=8E$fileRead =3D fopen($file, 'r'); >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=20 >> 3=EF=BC=8E$row =3D 1; >> 4=EF=BC=8E$twoDarray =3D array(); >> 5=EF=BC=8Ewhile (($line =3D fgetcsv($fileRead, 0, "\t")) !=3D=3D FALSE)= {=20 > //0 means >> I can read row whatever its length >> 6=EF=BC=8E=C2=A0 =C2=A0 if($row =3D=3D 1){ $row++; continue; } //skip h= eader >> 7=EF=BC=8E=C2=A0 =C2=A0 $line =3D implode(" ",$line). "\n"; >> 8=EF=BC=8E=C2=A0 =C2=A0 $twoDarray[] =3D $line; >> 9=EF=BC=8E} >> 10=EF=BC=8Efclose($fileRead); >>=20 >> Then I passed that twoDarray to pg_copy_from. >>=20 >> $con=3Dpg_connect("host=3D$host dbname=3D$dbname port=3D5432 user=3D$us= er >> password=3D$password"); >>=20 >> 11=EF=BC=8Eif (!$con) { >> 12=EF=BC=8E=C2=A0 =C2=A0 die("Couldn't open..
\n"); >> 13=EF=BC=8E} >>=20 >> 14=EF=BC=8Eif (pg_copy_from($con, $tableName, $twoDarray) !=3D=3D FALSE= ) { >> 15=EF=BC=8E=C2=A0 =C2=A0 print "OK!"; >> 16=EF=BC=8E} >> 17=EF=BC=8Eelse{ >> 18=EF=BC=8E=C2=A0 =C2=A0 print "Not OK.";=C2=A0 =C2=A0=20 >> 19=EF=BC=8E} >>=20 >> When I run the program, I have this error: >> Warning: pg_copy_from(): Copy command failed:=20 >> ERROR: value too long for type character varying(32) CONTEXT: COPY >> test_table, line 1, column rec_no: "1 Denny's orange juice 1,500=20 > yen >> "Dear John" 32" TV" in testProgram.php line xx. >>=20 >> My table definition is: >> CREATE TABLE test_table ( >> rec_no VARCHAR(32) PRIMARY KEY NOT NULL, >> item1 VARCHAR(255),..item2 .. until item5.); >>=20 >> Obviously, my program thinks everything in the data row is for the >> first field. >> No, no. >>=20 >> How to make it think that=20 >> 1 is for the 'rec_no' field, >> Denny's is for the 'item1' field,=20 >> orange juice is for the 'item2' field, >> 1,500 yen is for the 'item3' field, >> "Dear John" is for the 'item4' field and=20 >> 32" TV is for the 'item5' field? >>=20 >> When I tried removing '0' from line 5, that is,=20 >> while (($line =3D fgetcsv($fileRead, "\t")) !=3D=3D FALSE) {=20 > //without read >> length >>=20 >> I can see data written in the database, but with some extra double >> quotes and a missing comma! >> That is data was saved as >> Denny's,=C2=A0=C2=A0=C2=A0 orange juice,=C2=A0=C2=A0=C2=A0 "1 500 yen",= =C2=A0=C2=A0=C2=A0=20 > """Dear John""", and=20 >> =C2=A0=C2=A0=C2=A0 "32"" TV" >> into the respective fields. >> I cannot have those extra double quotes, and I cannot have missing >> comma in my data.=20 >=20 >=20 >=20 > fgetscsv returns an array from reading a record from a text file. > So=C2=A0 $line =3D fgetcsv($fileRead, 0, "\t", '"') would=20 > use tab as the > delimiter and remove any enclosure character equal to ". >=20 > I don't use Excel. With Libreoffice you can set the delimiter to the > pipe character and tell it not to use enclosures. So I don't know how > to massage your Excel file so that 32" TV is rendered correctly.=20 >=20 > Secondly, in PHP implode takes an array and turns it into a string with > a specified character used to delimit the values from the array. >=20 > I can only suggest that you read the PHP manual, as well as do some > searches for the use of pg_copy_from, although I doubt anything useful > will turn up.=20 >=20 > Cheers, > Rob >=20