PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 31st, 2004, 04:05 PM
rishijd rishijd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 303 rishijd User rank is Lance Corporal (50 - 100 Reputation Level)rishijd User rank is Lance Corporal (50 - 100 Reputation Level)rishijd User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 13 h 22 m 56 sec
Reputation Power: 5
Postgresql COPY command

Hi,

I have a stock.csv file for my "stock" table, it comes from MS-SQL and must be transferred to Postgresql.

The command i run is:

COPY stock FROM 'mydir/Stock.csv';

BTW the table 'stock' schema (part of it) is:

ClientCode varchar(3) NOT NULL ,
Sku varchar(20) NOT NULL ,
AlternateSkuCode varchar(20) NULL ,
StyleCode varchar(4) NULL ,
BarcodeCode varchar(20) NULL ,
Description varchar(60) NOT NULL ,
DetailedDescription varchar(255) NULL ,
TechnicalDescription varchar(255) NULL ,

1)
My first Stock.csv file had headers as the column names on its first line.
e.g.

Code:
"ClientCode","Sku","AlternateSkuCode",etc...


and I received this error:

Code:
ERROR: value too long for type character varying(3)
CONTEXT: COPY stock, line 1, column clientcode: ""ClientCode", "Sku", "AlternateSkuCode","StyleCode","BarcodeCode","Description","DetailedDescription",..."



So I thought - I better remove these column name headers.

2)
After removing the column names, the beginning of the first line of the Stock.csv file is now:

(part of it - the entire line is too long to paste)
Code:
"RJD","016","","","","Olga","Simply styled timeless boot with elegant toe 
and heel - an asset for any wardrobe.","","","","","","",


and I once again received a similar error:

Code:
ERROR: value too long for type character varying(3)
CONTEXT: COPY stock, line 1, column clientcode: ""RJD","016","","","","Olga","Simply styled timeless boot with elegant toe "


BTW I dont know if its a problem with "clientcode" or a problem with the detailedDescription field... because I'm looking at the CSV file that was sent to me and the detailed description had a line break (as I far as I can see using Wordpad) between 'toe' and 'and'.

This process is supposed to be automatic and, assuming the stock.csv file is uploaded to the correct location (FTP) intermittently, the Postgres database will also update its 'stock' table with a cron job.

Anyway the error mentions clientcode, but I dont understand it...there are only 3 letters..

If someone has any advice please let me know,
thanks very much!!!!

Reply With Quote
  #2  
Old August 1st, 2004, 02:29 AM
rishijd rishijd is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 303 rishijd User rank is Lance Corporal (50 - 100 Reputation Level)rishijd User rank is Lance Corporal (50 - 100 Reputation Level)rishijd User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 2 Days 13 h 22 m 56 sec
Reputation Power: 5
I was experimenting with different values and stuff and found that if I remove the double quotes from RJD

i.e.

Code:
RJD,"016","","","",

etc.

the value RJD is going into the database column clientcode (which is varchar(3)).
So great!
BUT the stock.csv file is supplied to me with double quotes... so is there any way of telling the POSTGRESQL COPY command not to enter the double quotes into the database as well?

The other quoted fields were put into the database with the double quote symbols as well! Which isnt good

The command I ran to copy:

Code:
copy stock from '/mydir/Stock.csv' using delimiters ',';


Does anyone know how to solve this double quote problem??
Much appreciated!

Reply With Quote
  #3  
Old August 1st, 2004, 09:41 AM
rycamor rycamor is offline
Gödelian monster
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 1999
Location: Pembroke Pines, Florida, USA
Posts: 2,300 rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level)rycamor User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 3 h 12 m 27 sec
Reputation Power: 56
Unfortunately, PostgreSQL does not import CVS data with quotes. Also, I believe it will not import a CSV file if it has standard Windows line terminators of CR/LF (or "\r\n"). It will only accept data with Unix-style newline (\n), and delimited data cannot have quotes (for example, if there is a comma inside a delimited field, it must be escaped with a slash, as in \,). I'm not absolutely certain about the Windows line terminator, though.

The COPY command defaults to expecting a tab-delimited format, so you might want to just export as tab-delimited from MSSQL, specifying no quotes around fields (I can't remember if MSSQL automatically uses quotes). If PostgreSQL still won't accept the data, make sure that all line terminators or tabs inside the data is properly escaped with the \ slash, and possibly change the line endings to the Unix-style newline. (see COPY documentation).

Also see this newsgroup thread about this problem.
__________________
The real n-tier system:

FreeBSD -> PostgreSQL -> [any_language] -> Apache -> Mozilla/XUL

Amazon wishlist -- rycamor (at) gmail.com

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Postgresql COPY command


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT