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 1gYu2a-00059y-O4 for pgsql-general@arkaria.postgresql.org; Mon, 17 Dec 2018 14:38:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gYu2Z-0006XW-Ab for pgsql-general@arkaria.postgresql.org; Mon, 17 Dec 2018 14:38:39 +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 1gYu2Y-0006XI-TB for pgsql-general@lists.postgresql.org; Mon, 17 Dec 2018 14:38:39 +0000 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gYu2R-0006zj-PT for pgsql-general@lists.postgresql.org; Mon, 17 Dec 2018 14:38:37 +0000 Received: by mail-pg1-x52c.google.com with SMTP id c25so4715408pgb.4 for ; Mon, 17 Dec 2018 06:38:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=O8gfdWC3QjFfVRAmltsvNwIXVbWzYDLwdFErpAk82xw=; b=pphAuiDvDMP3emXG58iux+1wzY8BDwCrgqdKxbPuO++5SqIEe5VHkLipL3xm04ejvk NBa6UBiou8FI4VXu9sAkxUpf5wuK4xBwmU0nLLPpvGBgtvSSG9c/1fxfit0mWcXk8h+a RBY/5e25RqbBsuC12/AhviE58w3cVRf7mKkmewWP0WgUEYyx4jL1so0Dj5jrhOFI40uB B04rcLrQ+0ZZQ4w1m7vR+W4g46btVXUDlGcLrZk4BNIzCfrYFYvKnfS7omuE4E9Ou+hJ w8owo+pnFdBeE9gtwMxN6ksmFgbaFD3Ktka6SD8+HV7oei73VltUau3pTGObj54OV0ld XZAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=O8gfdWC3QjFfVRAmltsvNwIXVbWzYDLwdFErpAk82xw=; b=GhdCD4+vluB7WBzNBIRG4KmMzMojSWx/WOmZySAVhM7MkjuCB5iAmYh5OmEblR98B6 Y/y4O4BsCkFQtV9yYOHZFrphzT7mLlPzuXPFyrWPOniKu/M2HgcRJDu4hQvMt+pT3NCw SD6wpmGG6/j7EOm6iaZOgsabBcHFMtiReFXhPJJ8KFY4fBTQOIGUJ+K8ZM45KFhdNJHk Jxq0U+9yzcBg0iq2+GMEq1k/VD1YCCRMCKUdDnJfMS0dwoaCz5KieALeXWe0hTCUCzHg uhDH3arrr7IiJ+z1KSSR/Dlqm/MVPI7RnTRPSpnLcfXHw4kMQdPFqCTiCSyIteCIVEjx BMEA== X-Gm-Message-State: AA+aEWbOpjcb1Jaijki8mIKMRIUeVISEPOL7qbnkTu5ngCBC1n+bhdI+ tkRGA8x7yQYyn0f7l5/AZL5nhMAS X-Google-Smtp-Source: AFSGD/Wn1OSOplGnEJPR+WBsBrkWGSIMzGVhsqDFWOx709Nssqd8z1QvePe1J6d1sjrWu8ydzu74Hw== X-Received: by 2002:a62:3c1:: with SMTP id 184mr13217558pfd.56.1545057510330; Mon, 17 Dec 2018 06:38:30 -0800 (PST) Received: from roblaptop (193-119-121-58.tpgi.com.au. [193.119.121.58]) by smtp.gmail.com with ESMTPSA id 202sm25037694pfy.87.2018.12.17.06.38.28 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 17 Dec 2018 06:38:29 -0800 (PST) Message-ID: Subject: Re: Creating 2D arrays for pg_copy_from, reading tab-delimted text file that contains comma and double quotes From: rob stone To: s400t@yahoo.co.jp, "pgsql-general@lists.postgresql.org" Date: Tue, 18 Dec 2018 01:38:24 +1100 In-Reply-To: <904538479.543179.1545050043907.JavaMail.yahoo@mail.yahoo.co.jp> References: <904538479.543179.1545050043907.JavaMail.yahoo.ref@mail.yahoo.co.jp> <904538479.543179.1545050043907.JavaMail.yahoo@mail.yahoo.co.jp> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.30.3-1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, On Mon, 2018-12-17 at 21:34 +0900, s400t@yahoo.co.jp wrote: > 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 will > accept and process 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 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 item1 item2 item3 item4 item5 > 1 Denny's orange juice 1,500 yen "Dear John" 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, double 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 item4 item5 > 1 Denny'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 Notepad. > > Then I created a two-dimensional array with PHP: > > 1.$file = 'test.txt'; //tab delimited file > 2.$fileRead = fopen($file, 'r'); > > 3.$row = 1; > 4.$twoDarray = array(); > 5.while (($line = fgetcsv($fileRead, 0, "\t")) !== FALSE) { //0 means > I can read row whatever its length > 6. if($row == 1){ $row++; continue; } //skip header > 7. $line = implode(" ",$line). "\n"; > 8. $twoDarray[] = $line; > 9.} > 10.fclose($fileRead); > > Then I passed that twoDarray to pg_copy_from. > > $con=pg_connect("host=$host dbname=$dbname port=5432 user=$user > password=$password"); > > 11.if (!$con) { > 12. die("Couldn't open..
\n"); > 13.} > > 14.if (pg_copy_from($con, $tableName, $twoDarray) !== FALSE) { > 15. print "OK!"; > 16.} > 17.else{ > 18. print "Not OK."; > 19.} > > 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 program 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, > orange juice is for the 'item2' field, > 1,500 yen is for the 'item3' field, > "Dear John" is for the 'item4' field and > 32" TV is for the 'item5' field? > > When I tried removing '0' from line 5, that is, > while (($line = fgetcsv($fileRead, "\t")) !== FALSE) { //without read > length > > 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, orange juice, "1 500 yen", """Dear John""", and > "32"" TV" > into the respective fields. > I cannot have those extra double quotes, and I cannot have missing > comma in my data. fgetscsv returns an array from reading a record from a text file. So $line = fgetcsv($fileRead, 0, "\t", '"') would use tab as the delimiter and remove any enclosure character equal to ". 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. 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. 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. Cheers, Rob