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 1gYs6C-0007g1-S5 for pgsql-general@arkaria.postgresql.org; Mon, 17 Dec 2018 12:34:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gYs6B-000215-Jf for pgsql-general@arkaria.postgresql.org; Mon, 17 Dec 2018 12:34:15 +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 1gYs6A-00020y-By for pgsql-general@lists.postgresql.org; Mon, 17 Dec 2018 12:34:15 +0000 Received: from nh602-vm11.bullet.mail.ssk.yahoo.co.jp ([182.22.90.36]) by makus.postgresql.org with smtp (Exim 4.89) (envelope-from ) id 1gYs65-00045U-A6 for pgsql-general@lists.postgresql.org; Mon, 17 Dec 2018 12:34:13 +0000 Received: from [182.22.66.105] by nh602.bullet.mail.ssk.yahoo.co.jp with NNFMP; 17 Dec 2018 12:34:04 -0000 Received: from [182.22.91.204] by t603.bullet.mail.ssk.yahoo.co.jp with NNFMP; 17 Dec 2018 12:34:04 -0000 Received: from [127.0.0.1] by omp607.mail.ssk.yahoo.co.jp with NNFMP; 17 Dec 2018 12:34:04 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 858852.5887.bm@omp607.mail.ssk.yahoo.co.jp X-YMail-OSG: b2bjGfoVM1mrGzPT15q6dF6xqbi2hY09SkmUkdqfBsa3P99fJylI0IkirXa_MtH Mj0jEZ2H77E7OYmvDgVQw5_YA.dTriswGwJrTNzm1k6Ma9EWlKbXPBEDO9_L1P2jeAiLAchAEJYG 8sMV5eNUFd5RINHInxxDpyVaphp4fQI07A8sq2nO44j5A9r0HbsdudU_nJtXHooK0xx6ery1jvqg ftUwX_3TGJVwWF0ovg4BuM5Iee5n7nVnNjK3ygv8X8dyyoiWH1aSVFmwvs8876Qp1ONkN4KW6CD0 qq73XcXXuKSB1SyBLOL7lJqyk.IjRjh3LehxG8b.E_MvObkF_XvbrY2Ce8nrac403vkYLp.5A.Yc D2LA1NCMXapIVB7JJ6Fs..sbZ0IaQ6n4UQzU1ZPYnQ1L.98NTIuWT3qe3OHb04Vub7dQcJ07lKtE d2Jy6aGaWaROevPx6NmxcpUsQv1RwrhTo8tKwSG.y1le0WwH5ILhMI3z3V5UF1X5C1SNeckipkrz 5uJStcE9fSu4zGpOPuG7FlXb5stlkqavk08oCQLx1mwnEC8ErLXrOXKQfe62HFp1SGWDDvqBeuNE _lHnGwvW3LimQZKeCgYN6TWmmfZF0q3E3ZcxLgpQTEg-- Received: from jws700102.mail.ssk.yahoo.co.jp by sendmailws616.mail.ssk.yahoo.co.jp; Mon, 17 Dec 2018 21:34:04 +0000; 1545050044.308 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1545050044; s=yj20110701; d=yahoo.co.jp; h=Date:From:Reply-To:To:Message-ID:Subject:MIME-Version:Content-Type:References; bh=I31mzozR4DUsrxr49jW+VtKcL/q0xUNQYNlmMZ9f0eI=; b=kFBQz43Dya839XsP2VzU6IDSuDNkLi79ffIGDvDHVv4C7vf9wJMLIxemyxnPE7XK d5zsax50bP9Jq1FW7q+aS+Cn03r8oll4reIMXZgMCJFDbNlMCCFOV32mvM4kmLEVoRx vAtMQANrTqrhhCsFrhNa9iynNSACPDF5DyEnuGD4= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=yj20110701; d=yahoo.co.jp; h=Date:From:Reply-To:Message-ID:MIME-Version:Content-Type:References; b=o/47rr2K+fsJALR1Cx9Ti+ldew3Hix5KfXtdmdzbSZdDZxeK+UN8ZKPuy8r53LFX T6mBdejGAa7K+exlS3+wVyhzpQ/uJ/4BP6ScxGuZUEOpyS2ibT28mJNUpNCYUbaWoaW RfjuWMTzum1cHnOqxfufGcbXelDWVKXUu8k7n4rQ=; X-yjpVirusScan: Scanned Date: Mon, 17 Dec 2018 21:34:03 +0900 (JST) From: Reply-To: To: "pgsql-general@lists.postgresql.org" Message-ID: <904538479.543179.1545050043907.JavaMail.yahoo@mail.yahoo.co.jp> Subject: Creating 2D arrays for pg_copy_from, reading tab-delimted text file that contains comma and double quotes MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_543176_2102762482.1545050043904" References: <904538479.543179.1545050043907.JavaMail.yahoo.ref@mail.yahoo.co.jp> Content-Length: 10954 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------=_Part_543176_2102762482.1545050043904 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hello Good People of the Forum! I am trying to insert some data into a PostgreSQL database using PHP and st= ruggling to create an array so that pg_copy_from function will accept and p= rocess the data. 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 crea= te the data and save it as tab delimited text file, and then convert encodi= ng to UTF-8 (from Excel's ANSI) before passing it to the PHP.=20 Here is my dummy data in Excel:rec_no=C2=A0=C2=A0=C2=A0 item1=C2=A0=C2=A0= =C2=A0 item2=C2=A0=C2=A0=C2=A0 item3=C2=A0=C2=A0=C2=A0 item4=C2=A0=C2=A0=C2= =A0 item5 1=C2=A0=C2=A0=C2=A0 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 "Dear John"=C2=A0=C2=A0=C2=A0 32" TV 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, d= ouble quotes and unpaired double quote (32" TV). When I save that Excel as a tab delimited text file, I get this:rec_no=C2= =A0=C2=A0=C2=A0 item1=C2=A0=C2=A0=C2=A0 item2=C2=A0=C2=A0=C2=A0 item3=C2=A0= =C2=A0=C2=A0 item4=C2=A0=C2=A0=C2=A0 item5 1=C2=A0=C2=A0=C2=A0 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 """Dear John"""=C2=A0=C2=A0=C2=A0 "32"" T= V"(As seen when I opened that file with Notepad) Because my data also contains non-ascii, I saved the file with UTF-8 encodi= ng using the Notepad. Then I created a two-dimensional array with PHP: 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 =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) { /= /0 means I can read row whatever its length 6=EF=BC=8E=C2=A0=C2=A0=C2=A0 if($row =3D=3D 1){ $row++; continue; } //skip = header 7=EF=BC=8E=C2=A0=C2=A0=C2=A0 $line =3D implode(" ",$line). "\n"; 8=EF=BC=8E=C2=A0=C2=A0=C2=A0 $twoDarray[] =3D $line; 9=EF=BC=8E} 10=EF=BC=8Efclose($fileRead); Then I passed that twoDarray to pg_copy_from. $con=3Dpg_connect("host=3D$host dbname=3D$dbname port=3D5432 user=3D$user p= assword=3D$password"); 11=EF=BC=8Eif (!$con) { 12=EF=BC=8E=C2=A0=C2=A0=C2=A0 die("Couldn't open..
\n"); 13=EF=BC=8E} 14=EF=BC=8Eif (pg_copy_from($con, $tableName, $twoDarray) !=3D=3D FALSE) { 15=EF=BC=8E=C2=A0=C2=A0=C2=A0 print "OK!"; 16=EF=BC=8E} 17=EF=BC=8Eelse{ 18=EF=BC=8E=C2=A0=C2=A0=C2=A0 print "Not OK.";=C2=A0=C2=A0=C2=A0=20 19=EF=BC=8E} When I run the program, I have this error:Warning: pg_copy_from(): Copy com= mand failed:=20 ERROR: value too long for type character varying(32) CONTEXT: COPY test_tab= le, line 1, column rec_no: "1 Denny's orange juice 1,500 yen "Dear John" 32= " TV" in testProgram.php line xx. My table definition is:CREATE TABLE test_table ( rec_no VARCHAR(32) PRIMARY KEY NOT NULL, item1 VARCHAR(255),..item2 .. until item5.); Obviously, my program thinks everything in the data row is for the first fi= eld.No, no. 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? When I tried removing '0' from line 5, that is,=20 while (($line =3D fgetcsv($fileRead, "\t")) !=3D=3D FALSE) {=C2=A0//without= read length I can see data written in the database, but with some extra double quotes a= nd a missing comma!That is data was saved asDenny's, orange juice, "1 500 y= en", """Dear John""", and=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 ------=_Part_543176_2102762482.1545050043904 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hello Good People of the Forum!

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 w= ill accept and process the data.

I can insert data= but not the way I want- my data this case contains comma, space, double qu= otes 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.

=
Here is my dummy data in Excel:
rec_no  &nbs= p; item1    item2    item3    = item4    item5
1    Denny's  &nb= sp; orange juice    1,500 yen    "Dear John"&= nbsp;   32" TV

Explanation: the first ro= w is header.
The second row is data for each column.
I = tried to create variation like apostrophe, space between words, comma, doub= le quotes and unpaired double quote (32" TV).

When= I save that Excel as a tab delimited text file, I get this:
rec_= no    item1    item2    item3&= nbsp;   item4    item5
1    Denn= y's    orange juice    "1,500 yen"  = ;  """Dear John"""    "32"" TV"
(As seen when= I opened that file with Notepad)

Because my data = also contains non-ascii, I saved the file with UTF-8 encoding using the Not= epad.

Then I created a two-dimensional array with = PHP:

1=EF=BC=8E$file =3D 'test.txt'; //tab delimited file 2=EF=BC=8E$fileRead =3D fopen($file, 'r');
     &nb= sp;     
3=EF=BC=8E$row =3D 1;
4=EF=BC=8E$twoDar= ray =3D array();
5=EF=BC=8Ewhile (($line =3D fgetcsv($fileRead, 0, "\t")= ) !=3D=3D FALSE) { //0 means I can read row whatever its length
6=EF=BC= =8E    if($row =3D=3D 1){ $row++; continue; } //skip header<= br>7=EF=BC=8E    $line =3D implode(" ",$line). "\n";
8=EF= =BC=8E    $twoDarray[] =3D $line;
9=EF=BC=8E}
10=EF=BC= =8Efclose($fileRead);

Then I passed that twoDarray= to pg_copy_from.

$con=3Dpg_connect("host=3D$host = dbname=3D$dbname port=3D5432 user=3D$user password=3D$password");

11= =EF=BC=8Eif (!$con) {
12=EF=BC=8E    die("Couldn't open..= <br>\n");
13=EF=BC=8E}

14=EF=BC=8Eif (pg_copy_from($con, $t= ableName, $twoDarray) !=3D=3D FALSE) {
15=EF=BC=8E    pri= nt "OK!";
16=EF=BC=8E}
17=EF=BC=8Eelse{
18=EF=BC=8E  &nb= sp; print "Not OK.";   
19=EF=BC=8E}

When I run the program, I have this error:
Warning: pg_copy= _from(): Copy command failed:
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 yen "Dear John" 32" TV" in testProgram.php line = xx.

My table definition is:
CREATE TABLE= test_table (
rec_no VARCHAR(32) PRIMARY KEY NOT NULL,
item1 VARCHAR(= 255),..item2 .. until item5.);

Obviously, my progr= am thinks everything in the data row is for the first field.
No, = no.

How to make it think that
1 is = for the 'rec_no' field,
Denny's is for the 'item1' field,
ora= nge juice is for the 'item2' field,
1,500 yen is for the 'item3' field,<= br>"Dear John" is for the 'item4' field and
32" TV is for the 'item5' f= ield?

When I tried removing '0' from line 5, that = is,
while (($line =3D fgetcsv($fileRead, "\t")) !=3D=3D FALS= E) { //without read length

I can see data wri= tten in the database, but with some extra double quotes and a missing comma= !
That is data was saved as
Denny's,=09= orange juice,=09"1 500 yen",=09"""Dear John""", and =09"32"" TV"
into the respective fields.
I cannot have those extra double quotes, and I cannot have = missing comma in my data.
------=_Part_543176_2102762482.1545050043904--