Thread: Unable to execute \copy from Client Application

Unable to execute \copy from Client Application

From
sujayr06
Date:
Hello All,

           My application is a C++ APP.

           I'm executing \copy command from the CPP and its as follows

           strPgSqlQuerybuf<<"\\copy hnb_registration FROM
'/root/Sujay/hnbfile.txt' USING DELIMITERS ','";

           Second \ is to keep the compiler happy. When this command
executes on PostGreSql,
           gives a syntax error as

           ERROR:  syntax error at or near "\" at character 1
           STATEMENT:  \copy hnb_registration FROM '/root/Sujay/hnbfile.txt'
USING DELIMITERS ','

           Can some please help!

           Thanks in advance.

Regards,
Sujay

--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Unable-to-execute-copy-from-Client-Application-tp5488816p5488816.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

Re: Unable to execute \copy from Client Application

From
Guillaume Lelarge
Date:
On Thu, 2012-02-16 at 00:07 -0800, sujayr06 wrote:
> Hello All,
>
>            My application is a C++ APP.
>
>            I'm executing \copy command from the CPP and its as follows
>
>            strPgSqlQuerybuf<<"\\copy hnb_registration FROM
> '/root/Sujay/hnbfile.txt' USING DELIMITERS ','";
>
>            Second \ is to keep the compiler happy. When this command
> executes on PostGreSql,
>            gives a syntax error as
>
>            ERROR:  syntax error at or near "\" at character 1
>            STATEMENT:  \copy hnb_registration FROM '/root/Sujay/hnbfile.txt'
> USING DELIMITERS ','
>
>            Can some please help!
>

\copy is a psql metacommand, not a SQL command. You cannot use it in
your application. You need to use the libpq API.


--
Guillaume
http://blog.guillaume.lelarge.info
http://www.dalibo.com


Re: Unable to execute \copy from Client Application

From
sujayr06
Date:
Hello Guillaume,

                   Please find the usage of \copy and the corresponding API.

                   strPgSqlQuerybuf<<"'\'copy hnb_registration FROM
'/root/Sujay/hnbfile.txt' USING DELIMITERS ','";

                   pPostGresQueryResult = PQexecParams(pPgConnection,
strPgSqlQuerybuf.str().c_str(),
                                                0,           // one param
                                                NULL,        // let the
backend deduce param type
                                                NULL,
                                                NULL,        // don't need
param lengths since text
                                                NULL,        // default to
all text params
                                                0);
                    ePostGresCmdExecStatus =
PQresultStatus(pPostGresQueryResult) ;

                    Let me know if the usage has to be corrected.
Regards,
Sujay



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Unable-to-execute-copy-from-Client-Application-tp5488816p5488935.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

Re: Unable to execute \copy from Client Application

From
sujayr06
Date:
Hello,

                   Please find the usage of \copy and the corresponding API.

                   strPgSqlQuerybuf<<"\\copy hnb_registration FROM
'/root/Sujay/hnbfile.txt' USING DELIMITERS ','";

                   pPostGresQueryResult = PQexecParams(pPgConnection,
strPgSqlQuerybuf.str().c_str(),
                                                0,           // one param
                                                NULL,        // let the
backend deduce param type
                                                NULL,
                                                NULL,        // don't need
param lengths since text
                                                NULL,        // default to
all text params
                                                0);
                    ePostGresCmdExecStatus =
PQresultStatus(pPostGresQueryResult) ;

                    Let me know if the usage has to be corrected.
Regards,
Sujay



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Unable-to-execute-copy-from-Client-Application-tp5488816p5488941.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

Re: Unable to execute \copy from Client Application

From
Guillaume Lelarge
Date:
On Thu, 2012-02-16 at 01:05 -0800, sujayr06 wrote:
> Hello,
>
>                    Please find the usage of \copy and the corresponding API.
>
>                    strPgSqlQuerybuf<<"\\copy hnb_registration FROM
> '/root/Sujay/hnbfile.txt' USING DELIMITERS ','";
>
>                    pPostGresQueryResult = PQexecParams(pPgConnection,
> strPgSqlQuerybuf.str().c_str(),
>                                                 0,           // one param
>                                                 NULL,        // let the
> backend deduce param type
>                                                 NULL,
>                                                 NULL,        // don't need
> param lengths since text
>                                                 NULL,        // default to
> all text params
>                                                 0);
>                     ePostGresCmdExecStatus =
> PQresultStatus(pPostGresQueryResult) ;
>
>                     Let me know if the usage has to be corrected.

Once again, you cannot use the \copy metacommand. Use the COPY API from
libpq (http://www.postgresql.org/docs/9.1/interactive/libpq-copy.html).


--
Guillaume
http://blog.guillaume.lelarge.info
http://www.dalibo.com


Re: Unable to execute \copy from Client Application

From
John R Pierce
Date:
On 02/16/12 1:05 AM, sujayr06 wrote:
>                     Please find the usage of \copy and the corresponding API.
>
>                     strPgSqlQuerybuf<<"\\copy hnb_registration FROM
> '/root/Sujay/hnbfile.txt' USING DELIMITERS ','";

The \ commands are all metacommands for the PSQL utility, none of it is
SQL that can be sent as a query.

you need to use the plain sql 'COPY ... FROM STDIN' command, and use
PQputCopyData() to send the data (eg, read your /root/Sujay/hnbfile.txt
and send it to that API).


ugh, /root ?!?   Why is ROOT running *ANY* SQL at all ??

--
john r pierce                            N 37, W 122
santa cruz ca                         mid-left coast