PostgreSQL:创建 .sql 文件以最快的方式将数据插入表中
我正在开发一个项目,我必须通过一些 C++ 函数解析一堆 .csv 文件,这些文件具有不同的格式并包含不同类型的数据。之后,我从文件中提取数据并创建一个 .sql 文件,该文件可以在 psql 中导入,以便稍后将数据插入到 PostgreSQL 数据库中。
但我无法找出 .sql 文件的正确语法。这是一个示例表和一个示例 .sql 文件,重现了我遇到的相同错误:
表创建代码:
CREATE TABLE "Sample_Table"
(
"Col_ID" integer NOT NULL,
"Col_Message" character varying(50),
CONSTRAINT "Sample_Table_pkey" PRIMARY KEY ("Col_ID" )
)
insertion.sql(在复制行之后,字段由单个制表符分隔)
copy Sample_Table (Col_ID, Col_Message) from stdin;
1 This is Spaaarta
2 Why So Serious
3 Baazinga
\.
现在,如果我执行上面的 sql 文件,我收到以下错误:
ERROR: syntax error at or near "1"
LINE 2: 1 This is Spaaarta
^
********** Error **********
如果有帮助,我正在运行 PostgreSQL 9.1 版本,并且上述所有查询都是通过 PGAdmin III 软件执行的。
I'm working on a project where I have to parse a bunch of .csv files, all of different formats and containing different kinds of data through some C++ functions. After that I extract data from the files and create a .sql file that can be imported in psql to insert the data into a PostgreSQL database at a later stage.
But I am not able to figure out the correct syntax for the .sql file. Here is a sample table and a sample .sql file reproducing the same errors I am getting:
Table Creation Code:
CREATE TABLE "Sample_Table"
(
"Col_ID" integer NOT NULL,
"Col_Message" character varying(50),
CONSTRAINT "Sample_Table_pkey" PRIMARY KEY ("Col_ID" )
)
insertion.sql (after the copy line, fields separated by a single tab character)
copy Sample_Table (Col_ID, Col_Message) from stdin;
1 This is Spaaarta
2 Why So Serious
3 Baazinga
\.
Now if I execute the above sql file, I get the following error:
ERROR: syntax error at or near "1"
LINE 2: 1 This is Spaaarta
^
********** Error **********
If it can help, I'm running a PostgreSQL 9.1 release, and all the above queries were executed through PGAdmin III Software.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PgAdmin 不支持以与 psql 相同的方式执行 COPY 命令(或者至少,我上次在 1.14 版本中尝试时不支持)。使用 psql 执行脚本,或使用 INSERT 语句。
PgAdmin doesn't support executing COPY commands in the same way that psql does (or at least, it didn't the last time I tried it with version 1.14). Use psql to execute the script, or use INSERT statements.
要检查三件事:
列之间实际上是否只有一个制表符?空格是不允许的。
还有更多错误消息吗?我至少缺少一个。 (见下文)
当您强制区分大小写的表和列名称时,您必须相应地执行此操作。因此,您必须这样写:
copy "Sample_Table" ("Col_ID", "Col_Message") from stdin;
否则您将收到这些错误:
有了这些,我就可以成功使用您的示例数据。
编辑错误更改:提问者现在有
“所以”
1
的内容不正确。我的猜测是,这是一个编码问题。带有 UTF-16 内容的 Windows 可能是罪魁祸首。
在其他网络上调试此类问题并不容易,因为对于许多半智能程序来说,他们大多数都喜欢调整“一些”东西。
但首先检查 psql 中的一些内容:
根据 Pastebin 数据,这些内容应该相同,并且是“SQL_ASCII”、“LATIN1”或“UTF-8”之一。
如果它们已经是或者如果调整它们没有帮助:Unix/Linux/cygwin 有一个
hexdump -C x.sql
程序,将其输出发布到 Pastebin。不要使用任何 Windows 编辑器(如 ultraedit)的十六进制转储 - 它们已经愚弄了我好几次。将文件传输到 Linux 时,请务必使用二进制传输。Three things to check:
Is there actually exactly one tab characters between the columns? Spaces are a no-go.
Are there more error messages? I'm missing at least one. (See below)
When you force case sensitive table and column names you have to do this consequently. Therefore you must write this:
copy "Sample_Table" ("Col_ID", "Col_Message") from stdin;
Otherwise you will get theese errors:
With these things in place I can use your example data successfully.
EDIT Bug change: The questioner now has
So something with the
1
is not OK.My guess is, that this is an encoding problem. Windows with it's UTF-16 stuff might be the culprit here.
Debugging these kind of problems other the web is not easy, because to many semi-intelligent programs are in the line, most of them like to adjust "a few" things.
But first check a few things in psql:
According to the pastebin data these should be the same and one of "SQL_ASCII", "LATIN1" or "UTF-8".
If they already are or if adjusting them does not help: Unix/Linux/cygwin has a
hexdump -C x.sql
program, post its output to pastebin. DO NOT USE the hexdump from any Windows editor like ultraedit - they have fooled me several times. When transferring the file to Linux be sure to use binary transfer.