sqlblobtofile
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| sqlblobtofile [2025/03/08 09:05] – jwan | sqlblobtofile [2025/10/10 10:15] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | Create | + | ** a progressbar |
| - | Create mysql connection prior to reading the file | + | ** Create mysql connection prior to reading the file |
| - | + | ||
| - | < | + | |
| - | procedure TForm1.btn1Click(Sender: | + | |
| - | var | + | |
| - | BinStream: TFileStream; | + | |
| - | Buffer: array[0..8191] of Byte; // 8KB buffer | + | |
| - | BytesRead, TotalBytesRead, | + | |
| - | BlobField: TBlobField; | + | |
| - | BlobStream: TStream; | + | |
| - | begin | + | |
| - | query.sql.clear; | + | |
| - | query.sql.text := ' | + | |
| - | query.active | + | |
| - | BlobField | + | |
| - | BlobSize | + | |
| - | BinStream | + | |
| - | PB.Max | + | |
| - | PB.Position | + | |
| - | + | ||
| - | TotalBytesRead := 0; | + | |
| - | BlobStream | + | |
| - | + | ||
| - | try | + | |
| - | while TotalBytesRead < BlobSize do begin | + | |
| - | | + | |
| - | if BytesRead = 0 then Break; | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | | + | |
| - | end; | + | |
| - | finally | + | |
| - | end; | + | |
| - | BlobStream.Free; | + | |
| - | BinStream.Free; | + | |
| - | end; | + | |
| - | </ | + | |
| Getting Size of BLOB (ie: file size): | Getting Size of BLOB (ie: file size): | ||
| < | < | ||
| - | SELECT OCTET_LENGTH(filecontent) AS filesize FROM picfile | + | SELECT OCTET_LENGTH(filecontent) AS filesize FROM picfile; |
| </ | </ | ||
| Line 49: | Line 12: | ||
| SELECT SUBSTRING(" | SELECT SUBSTRING(" | ||
| SELECT SUBSTRING(" | SELECT SUBSTRING(" | ||
| + | </ | ||
| + | If extracting binary file, need to do conversion to binary format rather than hex (0x) format. Do note that the chunk size should be x2 since the result in hexadecimal format wherein each character is in 2 character hexa code: | ||
| + | < | ||
| + | SELECT UNHEX(SUBSTRING(HEX(filecontent), | ||
| + | </ | ||
| + | Assign fstream as a global variable | ||
| + | < | ||
| + | var | ||
| + | Fstream: TFileStream; | ||
| + | </ | ||
| + | |||
| + | To get the Blob in chunks: | ||
| + | < | ||
| + | function Tformsbsystemloader.downloadsystem: | ||
| + | var | ||
| + | filepos | ||
| + | fullsize | ||
| + | partsize | ||
| + | s : string; | ||
| + | filename | ||
| + | begin | ||
| + | mysqld.Active | ||
| + | mysqld.SQL.text: | ||
| + | mysqld.Active | ||
| + | fullsize | ||
| + | filename | ||
| + | filepos | ||
| + | partsize | ||
| + | DeleteFile(filename); | ||
| + | if FileExists(FileName) then Fstream := TFileStream.Create(FileName, | ||
| + | else FStream := TFileStream.Create(FileName, | ||
| + | FStream.Seek(0, | ||
| + | repeat | ||
| + | | ||
| + | try | ||
| + | mysqld.active | ||
| + | // specify the chunk size, better divisible by 1K (1024) // | ||
| + | mysqld.SQL.Text := ' | ||
| + | mysqld.Active | ||
| + | | ||
| + | end; | ||
| + | s := mysqld.FieldValueByFieldName(' | ||
| + | | ||
| + | | ||
| + | until partsize =0; | ||
| + | Fstream.free; | ||
| + | result := GetFileSize(filename)=fullsize; | ||
| + | end; | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | procedure appendtoFile(const HexStr: string); | ||
| + | const | ||
| + | HexDigits = ' | ||
| + | var | ||
| + | I, Hi, Lo: Integer; | ||
| + | ByteValue: Byte; | ||
| + | Buffer: array of Byte; | ||
| + | begin | ||
| + | if Length(HexStr) mod 2 <> 0 then raise Exception.Create(' | ||
| + | SetLength(Buffer, | ||
| + | for I := 1 to Length(Buffer) do begin | ||
| + | Hi := Pos(LowerCase(HexStr[(I - 1) * 2 + 1]), HexDigits) - 1; | ||
| + | Lo := Pos(LowerCase(HexStr[(I - 1) * 2 + 2]), HexDigits) - 1; | ||
| + | if (Hi < 0) or (Lo < 0) then raise Exception.Create(' | ||
| + | ByteValue := (Hi shl 4) or Lo; | ||
| + | Buffer[I - 1] := ByteValue; | ||
| + | end; | ||
| + | Fstream.Write(Buffer[0], | ||
| + | end; | ||
| </ | </ | ||
sqlblobtofile.1741424746.txt.gz · Last modified: (external edit)
