ruby:使用正则表达式我想拆分sql语句

时间:2014-08-16 23:28:14

标签: ruby regex oracle11g oci8

我使用ruby和sinatra以及oci8并且我想运行一个包含多个sql语句的sql文件,但是oci8只能让我执行一个语句

str="INSERT INTO AA(NAME) VALUES ('asasa')"
@conn1 = OCI8.new(DB_USER, DB_PASSWORD, DB_SERVER)
@conn1.exec(str)

因为我必须使用正则表达式拆分所有sql语句,但我有一个问题,我确实找到了一个正则表达式让我这样做。我想拆分所有的陈述

sql文件的示例;

create table users (name varchar2(20),age int);
begin
insert into users values('amine5',20);
insert into users values('amine5',30);
end;

该文件可以包含所有sql语句

1 个答案:

答案 0 :(得分:0)

如果您只处理插入语句,那么这里有一个开始。

string2.0.0-p353 :001 > string =<<-EOS
2.0.0-p353 :002"> insert into users values('amine5','sdfsdf');
2.0.0-p353 :003"> begin
2.0.0-p353 :004"> insert into users values('amine5','sdfsdf');
2.0.0-p353 :005"> insert into users values('amine5','sdfsdf');
2.0.0-p353 :006"> end;
2.0.0-p353 :007"> insert into users values('amine5','sdfsdf');
2.0.0-p353 :008"> EOS
 => "insert into users values('amine5','sdfsdf');\nbegin\ninsert into users values('amine5','sdfsdf');\ninsert into users values('amine5','sdfsdf');\nend;\ninsert into users values('amine5','sdfsdf');\n" 
2.0.0-p353 :012 > string.scan(/insert.+;/i)
 => ["insert into users values('amine5','sdfsdf');", "insert into users values('amine5','sdfsdf');", "insert into users values('amine5','sdfsdf');", "insert into users values('amine5','sdfsdf');"] 

Rubular永久链接:http://rubular.com/r/Ffrd4swl4z