0
이 질문에 Multi-level parsing text 어떻게 파일을 텍스트 파일로 구문 분석 할 수 있습니까? 나는 조건을 더욱 복잡하게 만들고 데이터를 mysql 데이터베이스에 작성하기로 결정했다. 원래 데이터는 동일합니다. mysql 데이터베이스에 텍스트 파일 구문 분석 데이터를 삽입하십시오.
나는 옆에있는 텍스트 파일을 가지고 포함되어Head 1
Subhead 1
a 10
b 14
c 88
Subhead 2
a 15
b 16
c 17
d 88
Subhead 3
a 55
b 36
c 87
Head 4
Subhead 1
r 32
t 55
s 79
r 22
t 88
y 53
o 78
p 90
m 44
Head 53
Subtitle 1
y 22
b 33
Subtitle 2
a 88
g 43
r 87
Head 33
Subhead 1
z 11
d 66
v 88
b 69
Head 32
Subhead 1
n 88
m 89
b 88
Subhead 2
b 88
m 43
가 지금은 다음면에이 텍스트를 구성해야합니다. 나는이 데이터를 mysql 데이터베이스에 쓰고 싶다. 내 행동은 :
결과require 'tiny_tds'
current_head = ""
current_sub = ""
res = []
@host = 'server'
@user = 'user'
@pass = 'pass'
lines.each do |line|
case line
when /Head \d+/
current_head = line
when /Subhead/
current_sub = line
when /\w{1} 88/
num = line
res << "#{current_head}, #{current_sub}, #{num}"
conn = TinyTds::Client.new(:host => @host, :username => @user, :password => @pass)
insert_string = "insert into table (head, sub, num) VALUES (#{res})"
conn.execute(insert_string)
end
end
, 나는 데이터베이스에 하나의 라인을 삽입 할 수 있습니다. 또한 삽입 프로세스가 매우 느리며 프로세스 속도를 어떻게 높일 수 있습니까? 사실
head | Sub | num |
_________________________
Head 1 | Subhead 1| c 88 |
Head 1 | Subhead 2| d 88 |
Head 4 | Subhead 1| t 88 |
Head 53| Subhead 2| a 88 |
Head 33| Subhead 1| v 88 |
Head 32| Subhead 1| n 88 |
Head 32| Subhead 1| b 88 |
Head 32| Subhead 2| b 88 |
아마 각 루프 외부에 연결을 설정하겠습니까? – Fallenhero