简体   繁体   中英

JDBC PostgreSQL CopyManager.copyIn Returns Zero

I have the following code which always yields zero in the return value nRows .

CopyManager cm = new CopyManager((BaseConnection) connection);
long nRows = cm.copyIn
(
    "COPY myschema.mytable FROM STDIN (FORMAT csv)"
    , new StringReader(strRowData)
);

We verified that the data is in fact in the database, we just can't get the return value to work for logging purposes despite what the documentation says . Anybody has an idea?

EDIT: The fact that our myschema.mytable is actually referencing its sub tables, which are named in the form of myschema.mytable_[yyyymmdd] , may contribute to the issue.

EDIT2: I tried tweaking the database table so that it no longer references to any sub table, then the readings worked flawlessly. However, when I changed the table back to the way it was, the readings failed again. So apparently this is something to do at a lower level, either JDBC or even SQL. For the time being, I'll try to make a workaround to completely evade the issue.

This should work:

Long affectedRows = 0L;
CopyManager cm = new CopyManager((BaseConnection) connection);
CopyIn ci = cm.copyIn("COPY myschema.mytable FROM STDIN (FORMAT csv)");
ci.writeToCopy(bytes, 0, bytes.length);
affectedRows = ci.endCopy();

Can you try this :

COPY myschema.mytable FROM STDIN DELIMITER ',' QUOTE '''' HEADER CSV

http://www.postgresqltutorial.com/import-csv-file-into-posgresql-table/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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