简体   繁体   English

使用PostgreSQL时,使用COPY命令处理错误

[英]Handle error with COPY command when using PostgreSQL

I have a Python script that is executing the following command to copy the content of a CSV file to a database: 我有一个Python脚本执行以下命令将CSV文件的内容复制到数据库:

copy_query = "COPY table_name FROM STDIN DELIMITER '%s' CSV HEADER QUOTE '\"';" % (self.delimiter)

table_name represents an already created table with specific types. table_name表示已创建的具有特定类型的表。 The data in the CSV file doesn't always match with the column type. CSV文件中的数据并不总是与列类型匹配。 For example, the CSV file could be like this: 例如,CSV文件可能如下所示:

"row1", 123, "2012-11-11"
"row2", 346, "2012-11-12"
"row3", \N,  "2012-11-12"

As you can see, column 2 should be of type int , but since the data on row 3 doesn't match with type int , the entire operation fails. 如您所见,第2列应为int类型,但由于第3行上的数据与int类型不匹配,因此整个操作失败。 Is there a way to maybe reject this row altogether? 有没有办法可以完全拒绝这一行? I'd prefer it to fill in with some default value of the appropriate type, but rejecting the row outright is fine too. 我更喜欢用相应类型的一些默认值填写,但是完全拒绝该行也很好。 Thank you for your help! 谢谢您的帮助!

您不能使用COPY命令执行此操作,但您可以修改目标表以接受这些数字字段的NULL。

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

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