问题
When I run:
COPY con (date,kgs)
FROM 'H:Sir\\data\\reporting\\hi.rpt'
WITH DELIMITER ','
CSV HEADER
date AS 'Datum/Uhrzeit'
kgs AS 'Summe'
I get the error:
WARNING: nonstandard use of \\ in a string literal
LINE 2: FROM 'H:Sudhir\\Conair data\\TBreporting\\hi.txt'
^
HINT: Use the escape string syntax for backslashes, e.g., E'\\'.
I've been having this problem for quite a while. Help?
回答1:
It's not an error, it's just a warning. It has nothing to do with the file content, it's related to a PostgreSQL setting and the COPY
command syntax you're using.
You're using PostgreSQL after 8.1 with standard_conforming_strings
turned off - either before 9.1 (which defaulted to off) or a newer version with it turned off manually.
This causes backslashes in strings, like bob\ted
, get interpreted as escapes, so that string would be bob<tab>ted
with a literal tab, as \t
is the escape for a tab.
Interpreting strings like this is contrary to the SQL standard, which doesn't have C-style backslash escapes. Years ago the PostgreSQL team decided to switch to the SQL standard way of doing things. For backward compatibility reasons it was done in two stages:
Add the
standard_conforming_strings
option to use the SQL-standard interpretation of strings, but have it default to off. Issue warnings when using the non-standard PostgreSQL string interpretation. Add a newE'string'
style to allow applications to explicitly request escape processing in strings.A few releases later, turn
standard_conforming_strings
on by default, once people had updated and fixed the warnings their applications produced. Supposedly.
The escape for \
is \\
. So "doubling" the backslashes like you (or the tool you're using) done is correct. PostgreSQL is showing a warning because it doesn't know if when you wrote H:Sir\\data\\reporting\\hi.rpt
you meant literally H:Sir\\data\\reporting\\hi.rpt
(like the SQL spec says) or H:Sir\data\reporting\hi.rpt
(like PostgreSQL used to do, against the standard).
Thus there's nothing wrong with your query. If you want to get rid of the warning, either turn standard_conforming_strings
on , or add an explicit E''
to your string.
来源:https://stackoverflow.com/questions/24465965/copy-data-from-a-text-rpt-file-to-paste-it-postgresql-in-pgadmin