具有Xml String的MySql数据库表。如何将该数据导入另一个mysql表

时间:2014-11-19 16:51:58

标签: java mysql xml database

我有一个名为table的人。此表的列“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);

我们可以做任何商店程序吗?

1 个答案:

答案 0 :(得分:0)

最初的问题是。我有三张桌子。第一个表存在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

我厌倦了这个商店程序,但有一个问题。只有一个记录插入到父表中,并将其他记录插入到另一个子表中。 请纠正我错误的地方。