[yunqa.de] Re: store compressed text

  • From: Delphi Inspiration <delphi@xxxxxxxx>
  • To: yunqa@xxxxxxxxxxxxx
  • Date: Thu, 05 Jun 2008 17:52:56 +0200

luc de saeger wrote:

>I want to store this text into an sqlite db using disqlite components after i 
>compressed this text.
>
>What's the best way to do this ?

The DISQLite3 Wiki lists a few options to store compressed text:

  http://www.yunqa.de/delphi/doku.php/wiki/sqlite3/index

Alternatively, you can use the code from the simple console project below.

Ralf

---------------------------------------------------

{ Simple demo to save compressed text to a DISQLite3 database.

  Visit the DISQLite3 Internet site for latest information and updates:

    http://www.yunqa.de/delphi/

  Copyright (c) 2005-2008 Ralf Junker, The Delphi Inspiration <delphi@xxxxxxxx>

------------------------------------------------------------------------------ }

program DISQLite3_Compress_Text;

{$APPTYPE CONSOLE}
{$I DICompilers.inc}

uses
  {$IFDEF FastMM}FastMM4, {$ENDIF}
  SysUtils, ZLib, DISQLite3Api, DISQLite3Database;

//------------------------------------------------------------------------------

{ Procedure to compress a text string. Replace with your own code. }
procedure CompressString(
  const s: AnsiString;
  out sCompressed: Pointer;
  out sCompressedLen: Integer);
begin
  compressbuf(Pointer(s), Length(s), sCompressed, sCompressedLen);
end;

//------------------------------------------------------------------------------

{ Procedure to uncompress a text string. Replace with your own code. }
procedure UnCompressString(
  const sCompressed: Pointer;
  const sCompressedLen: Integer;
  out s: AnsiString);
var
  OutBuf: Pointer;
  OutBytes: Integer;
begin
  DecompressBuf(sCompressed, sCompressedLen, 0, OutBuf, OutBytes);
  SetString(s, PAnsiChar(OutBuf), OutBytes);
  FreeMem(OutBuf);
end;

//------------------------------------------------------------------------------

var
  DB: TDISQLite3Database;
  Stmt: TDISQLite3Statement;
  s8: AnsiString = ''; // Text in UTF-8.
  sCompressed: Pointer;
  sCompressedLen: Integer;
begin
  try

    { Create database and table. }
    DB := TDISQLite3Database.Create(nil);
    DB.DatabaseName := 'Test.db3';
    DB.CreateDatabase;
    DB.Execute('CREATE TABLE Compressed (t BLOB)');

    { Compress the text before insertion. }
    s8 := 'The text which should be compressed.';
    CompressString(s8, sCompressed, sCompressedLen);

    { Insert compressed text into table. }
    Stmt := DB.Prepare('INSERT INTO Compressed VALUES (?)');
    Stmt.Bind_Blob(1, sCompressed, sCompressedLen, sqlite_static);
    Stmt.Step;
    Stmt.Free;
    FreeMem(sCompressed);

    { Retreive compressed text from table. }
    Stmt := DB.Prepare('SELECT t FROM Compressed');
    while Stmt.Step = SQLITE_ROW do
      begin
        { Uncompress text and display. }
        sCompressed := Stmt.Column_Blob(0);
        sCompressedLen := Stmt.Column_Bytes(0);
        UnCompressString(sCompressed, sCompressedLen, s8);
        WriteLn(s8);
      end;
    Stmt.Free;

    DB.Free;

  except
    on e: Exception do
      begin
        WriteLn(e.Message);
        ReadLn;
      end;
  end;

  WriteLn;
  WriteLn('Done - Press ENTER to Exit');
  ReadLn;
end. 

_______________________________________________
Delphi Inspiration mailing list
yunqa@xxxxxxxxxxxxx
//www.freelists.org/list/yunqa



Other related posts: