【问题标题】:MySql database table having Xml String. How to Import that data into another mysql Table具有 XML 字符串的 MySql 数据库表。如何将该数据导入另一个mysql表
【发布时间】:2017-04-25 19:00:59
【问题描述】:

我有一个名为 table's 的人。此表的列“outsourcedData”包含以下 xml 作为字符串:

<person>
    <educations total="2">
        <education>
            <school-name>Delhi University</school-name>
            <degree>Master of Science (MSc)</degree>
            <field-of-study>Banking and Financial Support Services</field-of-study>
            <start-date>
                <year>2009</year>
            </start-date>
            <end-date>
                <year>2013</year>
            </end-date>
        </education>
        <education>
            <school-name>American University</school-name>
            <degree>Bachelor of Arts (BA)</degree>
            <field-of-study>Business Administration and Management, General</field-of-study>
        </education>
    </educations>
</person>

此表中有许多类似的行可用。有什么办法可以加载这些数据解析并插入到教育表中。

There are lots of row I am having in my database. But now I want to import this data into new table Education which I newly created in database corresponding fields with xml.(SchoolName,degree......).    

在 Mysql 数据库中,迁移此数据库的最佳方法是什么。
我被困在这个地方。请帮忙。帮助

create table person (id int,outersource varchar(1024));
insert into person values(1,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><person><educations total="2"><education><school-name>Delhi University</school-name><degree>Master of Science (MSc)</degree><field-of-study>Banking and Financial Support Services</field-of-study><start-date><year>2009</year></start-date><end-date><year>2013</year></end-date></education><education><school-name>American University</school-name><degree>Bachelor of Arts (BA)</degree><field-of-study>Business Administration and Management, General</field-of-study></education></educations></person>');
create table education( schoolName varchar(255), degree varchar(255),start_year datetime, end_year datetime);

任何我们可以做到这一点的存储过程?

【问题讨论】:

  • 只需在 JOIN 中进行更新,但很难知道您的问题是什么。
  • 我的一张表有数据。该表包含 XML 字符串以及我在此处显示的内容。我可以在这个表上触发选择查询。现在我创建了一个新表 Education。在这个新表中,我想迁移我以前的 XML 数据,这些数据在该表中以 XML 字符串形式提供
  • 在education表中新建一列然后INSERT INTO education(newColumn) SELECT oldColumn FROM MyOneTable
  • 我不想将该 xml 字符串重复到 Education 表中。我想解析那个 Xml 字符串,然后插入到 Education 表中。教育表中的一对一值。
  • 希望你现在能得到我的确切问题

标签: java mysql xml database


【解决方案1】:

原来的问题是这样的。我有三张桌子。存在 xml 字符串数据的第一个表和用于此 xml 字符串解析的两个新表。父表包含学校名称 + 用户 ID 唯一组合。在解析 xml 期间,如果任何节点包含与保存到子表中的数据相同的学校名称 + 用户 ID。 此子表具有此父表的引用。使用下面的存储过程。

CREATE DEFINER=`teneqs`@`localhost` PROCEDURE `xxx`()
begin
    declare v_row_index int unsigned default 0;
    declare v_row_count int unsigned;
    declare v_xpath_row varchar(255);
    declare userId int;
    declare userEduInfoId int;
    declare p_xml   text;
    declare p_xpath_row varchar(255) default '//educations[1]/education';
 declare done int;


DECLARE outsourcedUserDataCursor CURSOR FOR  select user_id, data from source_table where sourceType='LINKED_IN' order by user_id;
OPEN outsourcedUserDataCursor;
 outsourcedUserDataCursor_loop:
        LOOP FETCH outsourcedUserDataCursor INTO userId,p_xml;
       Set v_row_index := 0;
       -- SET done := 0;
       select userId,p_xml;
    -- calculate the number of row elements.
     set v_row_count := extractValue(
            p_xml
        ,   concat(
                'count('
            ,   p_xpath_row
            ,   ')'
            )
        );
select v_row_count as  "Education  Count" ;
        IF v_row_count > 0 THEN
            -- loop through all the row elements
            while v_row_index < v_row_count do       
                set v_row_index := v_row_index + 1;
                set v_xpath_row := concat(
                    p_xpath_row
                ,   '['
                ,   v_row_index
                ,   ']'
                );
                select v_row_index, v_xpath_row;
                begin                
                 DECLARE userEduInfoCursor CURSOR FOR  select id from parent where user_id= userId and school_name=extractValue(p_xml,concat(v_xpath_row,'/school-name[1]')) limit 1;
                 -- DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
                 select "Exist into Edu Info table: ",userId,extractValue(p_xml,concat(v_xpath_row,'/school-name[1]')) as 'SchoolName';                 
                 OPEN userEduInfoCursor;
                 -- IF done = 1 THEN
                        -- select done "Done";
                 -- END IF;
                 select 'userEduInfoCursor will be open';
                        LOOP
                        FETCH userEduInfoCursor INTO userEduInfoId;
                        select userEduInfoId;
                            IF userEduInfoId is NOT NULL then
                            select "in side if, userid is not null";
                            -- Insert UserEducationInfo
                                insert into child(
                                        a
                                        ,b
                                        ,c
                                        ,d
                                        ,e
                                        ,f
                                        ,g
                                        ,h
                                        ,i
                                        ,j
                                    ) values(
                                        userID
                                        ,userEduInfoId
                                        ,extractValue(p_xml,concat(v_xpath_row,'/school-name[1]'))
                                        ,'degree'
                                        ,'fieldOfStudy'
                                        ,'January'
                                        ,'2001'
                                        ,'December'
                                        ,'2014'
                                        ,'description');
                            ELSE
                            select "in side else";
                                close userEduInfoCursor;
                                -- User Info Inserted.
                                insert into parent (
                                    school_name
                                    ,user_id,
                                    creationDate,
                                    lastmodified) 
                                values (
                                    extractValue(p_xml,concat(v_xpath_row,'/school-name[1]'))
                                    ,userId
                                    ,now()
                                    ,now()
                                );

                                OPEN userEduInfoCursor;
                                FETCH userEduInfoCursor INTO userEduInfoId;
                                -- Detaild Information Entered

                              insert into child(
                                        a
                                        ,b
                                        ,c
                                        ,d
                                        ,e
                                        ,f
                                        ,g
                                        ,h
                                        ,i
                                        ,j
                                    ) values(
                                        userID
                                        ,userEduInfoId
                                        ,extractValue(p_xml,concat(v_xpath_row,'/school-name[1]'))
                                        ,'degree'
                                        ,'fieldOfStudy'
                                        ,'January'
                                        ,'2001'
                                        ,'December'
                                        ,'2014'
                                        ,'description');
                        END IF; 
                    END Loop;
                 close userEduInfoCursor;
                end;
                -- check userid  & school name already exist into info table -> insert into details  table. 

            end while;
        END IF;
    end LOOP outsourcedUserDataCursor_loop;
close outsourcedUserDataCursor;
end

我厌倦了这个存储过程,但有一个问题。只有一条记录插入到父表中,其他记录插入到另一个子表中。 请纠正我做错的地方。

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 2017-07-12
    • 2023-03-03
    • 2011-04-25
    • 1970-01-01
    • 2015-05-03
    • 2019-03-17
    • 1970-01-01
    相关资源
    最近更新 更多