Search Postgresql Archives

COPY TO STDOUT WITH (FORMAT CSV, HEADER), and embedded newlines

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi.

It's my first time using COPY TO. And first time using built-in CSV support.
Performs well. BUT...

The code below (real code, but using a custom libpq wrapper lib) is run on
a few tables, with unit tests that verify the number of lines of the
output file.
And for a few of those tables, there's a mismatch, the output from PostgreSQL
"has too many lines". I've tracked these to text values in the DB with embedded
newlines. These values are 'normal'. I'm not use to CSV, but I suppose
such newlines
must be encoded, perhaps as \n, since AFAIK CSV needs to be 1 line per row, no?

So how's one supposed to configure the CSV output for the DB with
embedded newlines?

Thanks, --DD

    auto rset = my_exec(*conn_, "COPY MY_TAB TO STDOUT WITH (FORMAT
CSV, HEADER)");
    if (rset.status() != PGRES_COPY_OUT) {
        raise("CSV Export via SQL COPY error: ", rset.error_msg());
    }

    std::ofstream os(file_name);
    bool done = false;
    while (!done) {
        auto buf = pq::CopyOutBuffer::get(*conn_);

        switch (buf.status()) {
        case pq::CopyOutStatus::eWait:  assert(false); continue;
        case pq::CopyOutStatus::eDone:  done = true; continue;
        case pq::CopyOutStatus::eError: raise("PQgetCopyData: {}",
conn_->error_msg());
        case pq::CopyOutStatus::eData:  break; // process it below
        }

        // Each buffer seems to a single line of output,
        // with Unix-newline at the end, on all platforms.
        os.write(buf.data(), buf.size());
    }

    os.close();





[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]

  Powered by Linux