简体   繁体   中英

CopyManager only delimit once

I'm trying to import a password list into a postgresql database. Is there any way to delimit a line only once or only at the first occurrence of the delimiter character?

Data to import:

demo@mail.ru:123
demo@mail.de:ab:c

My importing code:

import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;

CopyManager copyManager = new CopyManager((BaseConnection) connection);
FileReader fileReader = new FileReader(file);
copyManager.copyIn("COPY db FROM STDIN DELIMITER ':' ", fileReader);

This code will fail at the second example as it contains two : characters. Any ideas or suggestions to solve this?

Thanks for any help!

Suggestion to escape second : :

org.postgresql.util.PSQLException: ERROR: extra data after last expected column
  Where: COPY pwned, line 1: "user@demo.pl:123":abc"
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)
    at org.postgresql.core.v3.QueryExecutorImpl.processCopyResults(QueryExecutorImpl.java:1116)
    at org.postgresql.core.v3.QueryExecutorImpl.endCopy(QueryExecutorImpl.java:965)
    at org.postgresql.core.v3.CopyInImpl.endCopy(CopyInImpl.java:45)
    at org.postgresql.copy.CopyManager.copyIn(CopyManager.java:181)
    at org.postgresql.copy.CopyManager.copyIn(CopyManager.java:156)
    at TxtToPostgreSQL.FileToPostgreSQL.fileToPostgre(FileToPostgreSQL.java:22)
    at TxtToPostgreSQL.Import.main(Import.java:31)

The PostgreSQL COPY command supports the ESCAPE option. Use this together with escaping the colons in your data. Since ESCAPE defaults to QUOTE which defaults to " , change your data to this:

demo@mail.ru:123
demo@mail.de:ab":c

Also DELIMITER must be a single one-byte character. Your SQL should be

COPY db FROM STDIN DELIMITER ':' 

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