Java 具有Xml字符串的MySql数据库表。如何将该数据导入另一个mysql表
我有一个叫table的人。此表的列outsourcedData包含以下xml作为字符串:Java 具有Xml字符串的MySql数据库表。如何将该数据导入另一个mysql表,java,mysql,xml,database,Java,Mysql,Xml,Database,我有一个叫table的人。此表的列outsourcedData包含以下xml作为字符串: <person> <educations total="2"> <education> <school-name>Delhi University</school-name> <degree>Master of Science (MSc)</degree>
<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>
在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);
任何存储过程我们都可以这样做吗?最初的问题是。我有三张桌子。存在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
我厌倦了这个商店程序,但有一个问题。只有一条记录插入到父表中,而其他记录插入到另一个子表中。
请纠正我的错误。只需在JOIN中进行更新,但很难从您的问题中知道哪个是什么。我的一个表中有数据。该表包含XML字符串,正如我在这里展示的那样。我可以在此表上启动select查询。现在我创建了一个新的教育表。在这个新表中,我想迁移以前在该表中以XML字符串形式提供的XML数据在教育表中创建一个新列,然后插入教育新列从MyoneTable中选择oldColumn我不想在教育表中重复该XML字符串。我想解析那个Xml字符串,然后插入到教育表中。一对一的价值观进入教育表。希望你们现在能得到我的确切问题
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