由于初期规划不好,项目管理的action都存入到数据库中了,而实际上应该以配置文件的形式保存的,所以现在想改过来。一条条复制是不可能的,几百条记录,可以用java编写个小程序或者其他语言编写个脚本来实现,都很简单,但因为这两天在学存储过程,所以就试着写一个了(数据库的过程、函数和触发器等真的很好玩啊!)。
其实这个过程只有两个要点:
①如何从数据库中读取出所有action而且没有重复(因为之前很多失误操作,写入很多重复的记录);
②如何把读出来的集合转换成properties的格式并写入文件。
①的查询语句:
select C_ACTION,B_POWER from p_link where I_ID in
(select max(I_ID) from p_link group by C_ACTION);
②循环处理读出来的集合,拼装成一个大串,一次性写入文件:(使用光标来遍历集合)
repeat
fetch c_cursor into tmp_action,tmp_power;
if not i_done then
set c_content = concat(c_content, tmp_action, '=', tmp_power, char(13));
end if;
until i_done end repeat;
最终可得到一行行结构为 XXX=XXX 键值对。
详细代码如下:
drop procedure if exists get_all_links;
delimiter $$
create procedure get_all_links()
begin
declare c_content text;
declare i_done int default 0;
declare tmp_action varchar(100);
declare tmp_power tinyint;
declare c_cursor cursor for
select C_ACTION,B_POWER from p_link where I_ID in
(select max(I_ID) from p_link group by C_ACTION);
declare continue handler for sqlstate '02000' set i_done = 1;
open c_cursor;
set c_content = '';
repeat
fetch c_cursor into tmp_action,tmp_power;
if not i_done then
set c_content = concat(c_content, tmp_action, '=', tmp_power, char(13));
end if;
until i_done end repeat;
close c_cursor;
select c_content into outfile 'D:\\action.properties';
end
$$
delimiter ;
call get_all_links();
drop procedure get_all_links;