zoukankan      html  css  js  c++  java
  • PostgreSQL处理xml数据初步

    磨砺技术珠矶,践行数据之道,追求卓越价值
    回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页
    [作者 高健@博客园  luckyjackgao@gmail.com]

    PostgreSQL及PPAS支持xml数据类型,故进行如下的实验。

    先看PPAS9.2中的效果:

    先建立表:

    -bash-3.2$ ./bin/psql -d edb
    psql (9.2.1.3)
    "help" でヘルプを表示します.
    
    edb=# CREATE TABLE xmltab01(books xml);
    CREATE TABLE
    edb=# INSERT INTO xmltab01(books) VALUES ('<title>Book0001</title>');
    INSERT 0 1
    edb=# INSERT INTO xmltab01(books) VALUES ('<title>Book0002</title>');
    INSERT 0 1
    edb=#

    再查询:

    edb=# SELECT books from xmltab01;
    
    books 
    -------------------------
    <title>Book0001</title>
    <title>Book0002</title>
    (2 行)

    然后,继续插入更复杂的数据:

    edb=# INSERT INTO xmltab01(books) VALUES('
    
    edb'# <book>
    edb'# <title>Book0003</title>
    edb'# <author>Author0003</author>
    edb'# <publisher>Puber0003</publisher>
    edb'# </book>');
    INSERT 0 1
    edb=#

     

    edb=# INSERT INTO xmltab01(books) VALUES('
    
    edb'# <book>
    edb'# <title>Book0004</title>
    edb'# <author>Author0004</author>
    edb'# <publisher>Puber0004</publisher>
    edb'# </book>');
    INSERT 0 1
    edb=# 

     

    edb=# INSERT INTO xmltab01(books) VALUES('
    edb'# <book>
    edb'# <title>Book0005</title>
    edb'# <author>Author0004</author>
    edb'# <publisher>Puber0004</publisher>
    edb'# </book>');
    INSERT 0 1
    edb=# 

    接着,进行一个带条件的查询,查找出作者为Author0004的用户所写的书的title:

    不符合条件的记录也被查询出来,留下一个空值。

    edb=# SELECT xpath('/book[author/text()="Author0004"]/title', books) FROM xmltab01;
    xpath 
    ---------------------------
    {}
    {}
    {}
    {<title>Book0004</title>}
    {<title>Book0005</title>}
    (5 行)
    
    edb=#

    最后删除表:

    edb=# drop table xmltab01;
    DROP TABLE
    edb=#

     

    下面是使用PostgreSQL时的情形, 

    初次使用发生错误,需要重新编译数据库:

    postgres=# CREATE TABLE xmltab01(books xml);
    CREATE TABLE
    postgres=# INSERT INTO xmltab01(books) VALUES ('<title>Book0001</title>');
    ERROR:  unsupported XML feature
    LINE 1: INSERT INTO xmltab01(books) VALUES ('<title>Book0001</title>...
                                                ^
    DETAIL:  This functionality requires the server to be built with libxml support.
    HINT:  You need to rebuild PostgreSQL using --with-libxml.
    postgres=# 

    在进行了  ./configure --with-libxml  gmake && gmake install 之后,

    可以正常工作。

     

    [作者 高健@博客园  luckyjackgao@gmail.com]
    回到上一级页面:PostgreSQL基础知识与基本操作索引页    回到顶级页面:PostgreSQL索引页
    磨砺技术珠矶,践行数据之道,追求卓越价值

  • 相关阅读:
    SqlServer 查看数据库中所有存储过程
    SqlServer 查看数据库中所有视图
    SqlServer 查询表的详细信息
    SqlServer 遍历修改字段长度
    net core 操作Redis
    Tuning SharePoint Workflow Engine
    Open With Explorer
    Download language packs for SharePoint 2013
    Change Maximum Size For SharePoint List Template when Saving
    Six ways to store settings in SharePoint
  • 原文地址:https://www.cnblogs.com/gaojian/p/3314369.html
Copyright © 2011-2022 走看看