首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将XML数据导入postgres数据库表?

如何将XML数据导入postgres数据库表?
EN

Stack Overflow用户
提问于 2016-05-12 10:00:04
回答 1查看 5K关注 0票数 0

我已经为帐户表创建了XML格式文件,它使用以下方式创建:选择table_to_xml(' account ',true,FALSE,'');

-表结构为:创建表public.account ( account_id整数NULL,名称VARCHAR(1) NULL,类型VARCHAR(20),group_name VARCHAR(50),约束account_pkey主键(Account_id));

问题:如何使用PostgreSQL中的XML文件直接将数据加载到帐户表中?

EN

回答 1

Stack Overflow用户

发布于 2016-05-14 12:02:10

由于xml的转换,我不得不使用varchar(2)。

我使用了select into (创建public.account)

代码语言:javascript
运行
复制
select account_id::text::int, account_name::varchar(2),
account_type::varchar(20) , account_group::varchar(50) INTO
public.account from(
WITH x AS ( SELECT
'<accounts>
<account>
<account_id>1</account_id>
<account_name> A </account_name>
<account_type> off shore</account_type>
<account_group> slush fund </account_group>
</account>
<account>
<account_id>3</account_id>
<account_name> C </account_name>
<account_type> off shore</account_type>
<account_group> slush fund </account_group>
</account>
</accounts> '::xml AS t)

SELECT unnest(xpath('/accounts/account/account_id/text()', t)) 
AS account_id,
   unnest(xpath('/accounts/account/account_name/text()', t)) 
AS account_name,
   unnest(xpath('/accounts/account/account_type/text()', t)) 
AS account_type,
   unnest(xpath('/accounts/account/account_group/text()', t)) 
AS account_group

FROM   x) as accounts

如果您对读取xml文件感兴趣,那么这可能是有用的。

参考文献stackexchange sql to read xml from file into postgresql

我希望这能帮到你

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37183542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档