대량의 데이타 입력방법(blob)

ODBC를 이용해 대량 데이타 입력방법
(출처 : www.solo89.pe.kr의 database 게시판)

BLOB화일을 칼럼에 쓰고,
다시 BLOB 자료 칼럼에서 화일로 쓰기 작업입니다.

1) script에 아래와 같이 작성합니다.

// image화일을 DB Table Column에 넣기
// Column은 binary,image 자료.  
// FileRead()함수에서 Blob자료는 32765씩 읽는 제한이 있다.
// UpdateBlob 문장으로 컬럼에 쓰게한다.
long ll_flen
long loops
long Bytes_read
integer li_FileNum
integer li_pct_complete,i
Blob b, tot_b
string ls_sourcename

ls_sourcename = “autumn.bmp”  // 1.31MB (1,382,454)

// 원본크기
ll_flen = Filelength(ls_sourcename)
// 화일읽기
li_FileNum = FileOpen(ls_sourcename, streamMode!, Read!, lockRead! )

if ll_flen > 32765 then
if mod(ll_flen, 32765) = 0 then
// 나머지 값이 0으로 떨어지면 32765로
  // 나누어 정수로 만든다.    
  loops = ll_flen/32765          
else
// 나머지 값이 0 이하 소수점 일때는 1로 만든다.
loops = (ll_flen/32765)+1    
  end if
else
loops = 1
end if

li_pct_complete = 0
// 진행바 초기화
// uo_progress_bar.uf_set_position (li_pct_complete)

// 32765보다 크면  
for i = 1 to loops                      
// 한번에 32765씩 읽는다.
// 화일포인트는 최근 읽은 지점에 자동위치
Bytes_Read = FileRead(li_FileNum, b)
// Blob 변수에 누적                                
tot_b = tot_b + b  

// 10 % 단위 추출(진행 바) = ( 변수크기 / 전체 화일크기 ) * 100.0                  
// li_pct_complete = ( len(tot_b) / ll_flen ) * 100.0

// 10 % 단위로 보기(진행 바)
// uo_progress_bar.uf_set_position (li_pct_complete)

next
FileClose(li_FileNum)

// 쓰기
UPDATEBLOB  addres SET image_datatype = :tot_b  
WHERE code = ’01’
USING SQLCA ;

// 에러체크
IF SQLCA.SQLCode = 0 THEN
COMMIT USING SQLCA ;
messagebox(“Blob Column Read”,”성공!”)
ELSE
// SQLCode = -1, 100
  MessageBox(“Blob Column SQL error”, SQLCA.SQLErrText)
END IF

————————————————————
2)
// DB table column에서 image 읽어서 화일로 만들기
// SelectBlob문장으로 image 불러온다.
// FileWrite()함수는 32765씩 쓰기 제한이 있다.

integer li_fileNum,i
long loops
long ll_rtn
long bytes_read
blob tot_b
// 읽기
SELECTBLOB image_datatype
INTO  : tot_b
FROM addres
WHERE addres.code = ’01’
USING SQLCA;

IF SQLCA.SQLCode = 0 THEN
COMMIT USING SQLCA ;

ELSE
// SQLCode = -1,100
  MessageBox(“Blob Column SQL error”, SQLCA.SQLErrText)
END IF

// image 크기
bytes_read = len(tot_b)

if bytes_read > 32765 then
if Mod(Bytes_read,32765) = 0 then
loops = bytes_read/32765
else
loops = (bytes_read/32765) + 1
end if
else
loops = 1
end if

// 화일열기
li_FileNum = FileOpen( &
          “C:EMP_PICS.BMP”, &
   StreamMode!, Write!, Shared!, Replace!)

For i = 1 To loops
     ll_rtn = FileWrite( li_FileNum, tot_b)
    If ll_rtn = 32765 Then  
     tot_b = BlobMid( tot_b, 32766)
    End If
Next
// 닫기
FileClose(li_FileNum)
messagebox( “Blob File write”, “성공!” )

—————————————————
참고로 mysql  서버 실행시 “-O allowed_max_packet=2M”를 추가하거나 my.cnf에 추가해야 대용량 데이타를 전송할 수 있다.

Leave a Comment

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.