简体   繁体   English

从c ++程序执行时psql \\ copy命令错误

[英]psql \copy command error when executing from c++ program

ERROR:  syntax error at or near "\"
LINE 1: \copy mytable FROM urishFile WITH (FORMAT csv, DELIMITER ','...
        ^

I am getting this error when running command from c++ program. 从C ++程序运行命令时出现此错误。

command = "\\copy mytable FROM urishFile WITH (FORMAT csv, DELIMITER ',',  NULL 'NULL');";
executCommand(conn, command);

void executCommand(PGconn *conn, std::string command) {
    PGresult   *res;                                    // holds query result 
    res = PQexec(conn, command.c_str());
    if (PQresultStatus(res) != PGRES_COMMAND_OK) {
        fprintf(stderr, "%s", PQerrorMessage(conn));
        PQclear(res);
        exit_nicely(conn);
    }
    PQclear(res);       
}

When running the same command from psql prompt, it works without errors. 从psql提示符运行相同的命令时,它可以正常工作。

mydatabase=> \copy mytable FROM urishFile WITH (FORMAT csv, DELIMITER ',',  NULL 'NULL');

What I have done wrong? 我做错了什么?

\\copy is a psql command, not an SQL command. \\copypsql命令,不是SQL命令。 psql commands only work inside the psql shell. psql命令仅在psql shell中起作用。 Since you're talking directly to the database, you'd need to use the COPY SQL command. 由于您是直接与数据库对话,因此需要使用COPY SQL命令。 However, COPY usually works with the server's file system rather than the client's: 但是, COPY通常使用服务器的文件系统,而不是客户端的文件系统:

COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. 带有文件名的COPY指示PostgreSQL服务器直接从文件读取或写入文件。 The file must be accessible to the server and the name must be specified from the viewpoint of the server. 服务器必须可以访问该文件,并且必须从服务器的角度指定名称。 When STDIN or STDOUT is specified, data is transmitted via the connection between the client and the server. 当指定STDIN或STDOUT时,数据将通过客户端和服务器之间的连接进行传输。

You'll probably need to use the STDIN option and PQputCopyData to send the data over to the PostgreSQL server. 您可能需要使用STDIN选项和PQputCopyData将数据发送到PostgreSQL服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM