Postgresql:从文件中插入列的值 [英] Postgresql: inserting value of a column from a file
问题描述
例如,有一个名为testtable的表,它具有以下列:testint(integer)和testtext(varchar(30))。
(15,CONTENT_OF_FILE('file'));如果你的数据库中没有这个文件,
在阅读postgresql文档时,我能找到的是COPY TO / FROM命令,但是这个命令适用于表格,而不是单个列。
那么,我该怎么办?
代码是从您的编程语言中动态执行的,使用该语言的方式来读取文件,并执行一个普通的INSERT语句。
但是,如果这个SQL代码是要通过 psql
命令行工具执行,您可以使用以下构造:
\ set content`cat file`
INSERT INTO testtable VALUES(15,:'content');
请注意,该语法特定于 psql
,并使用 cat
shell命令。
PostgreSQL手册详细介绍:
For example, there is a table named 'testtable' that has following columns: testint (integer) and testtext (varchar(30)).
What i want to do is pretty much something like that:
INSERT INTO testtable VALUES(15, CONTENT_OF_FILE('file'));
While reading postgresql documentation, all I could find is COPY TO/FROM command, but that one's applied to tables, not single columns.
So, what shall I do?
If this SQL code is executed dynamically from your programming language, use the means of that language to read the file, and execute a plain INSERT statement.
However, if this SQL code is meant to be executed via the psql
command line tool, you can use the following construct:
\set content `cat file` INSERT INTO testtable VALUES(15, :'content');
Note that this syntax is specific to psql
and makes use of the cat
shell command.
It is explained in detail in the PostgreSQL manual:
这篇关于Postgresql:从文件中插入列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!