필터 지우기
필터 지우기

SQL Blob - fread and then insert?

조회 수: 4 (최근 30일)
Stan
Stan 2013년 6월 19일
Hello,
I am running into some problems with inserting local files to sql server.
When I retrieve a blob I get a huge vector that I can convert to a file with fwrite. So I figured, if I fread a file and then insert I will bypass the permissioning problem.
I put all my data I want to insert in a structure and insert it. Then I get: Error using database/insert (line 189) Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
So what do I need to do with the data from my fread so that the table column (which is varbinary(max)) accepts it?
Thank you.

채택된 답변

Stan
Stan 2013년 6월 20일
I have figured it out. Not a clean solution in any way though.
I read the file: fid=fopen(FileLocation); FileData=fread(fid, 'uint8'); fclose(fid);
Then I just use fastinsert. I have to put filedata in a cell.
fastinsert(conn, 'DocumentTable', {'FileDescription', 'FileType', 'Id', 'TimeSaved', 'FileData'},[{FileDescription}, {Data.FileType}, num2cell(Id), {Data.TimeSaved}, {Data.FileData}])
However,while this looks clean it is quite messy getting it out.
% Query='SELECT * FROM DocumentTable'; % data=fetch(conn, Query); % test=data.FileData; % Trial=double(test{1}); % Trial(Trial<0)=Trial(Trial<0)+256; % fid=fopen('rout.docx', 'w'); % fwrite(fid, Trial, 'uint8'); % fclose(fid)
I have no idea why I have to add 256 to negative numbers but it seems to work. Tested it for both pdf and docx. There must be a better solution but until I find that one I will use this. It bypasses some permission problem when using SELECT * FROM OPENROWS BULK statements.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Database Toolbox에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by