LoginSignup
6
0

More than 1 year has passed since last update.

Delphi / FireDAC MySQL と MariaDB で、画像の保存と取り出しのテストをしてみました。

Posted at

Delphi Advent Calendar 2022 12/05と12/07と12/08のの記事の MySQL と MariaDB  版です。

1. Ubuntu20.04 にMySQL8.xをインストーし直後の状態では、私のcnfの設定の知識不足で、接続不可となってしまったので、今回動作確認した環境は

Windows10にMySQL5.7.40 32bit
57_my.png
Windows10にMySQL8.0.31 64bit
80.png
Ubuntu20.04にMariaDB10.3.37 としました。
maria_ub.png

2. 事前に test_1 データベースを作成 MySQL Workbenchを使用

MySQL5.7.40 
57_wb_my57.png

MySQL8.0.31
80wkbn1.png

MariaDB10.3.37
警告が出ますが、接続はできるようです
Incompatible/nonstandard server version or connection protocol detected (10.3.37).
A connection to this database can be established but some MySQL Workbench features may not work properly since the database is not fully compatible with the supported versions of MySQL.
MySQL Workbench is developed and tested for MySQL Server versions 5.6, 5.7 and 8.0
wb_mariad10無題.png
maria_wkbch.png

3. MySQL と MariaDB では、データ型は BLOB です

他に TINYBLOB、 MEDIUMBLOB、 LONGBLOB が使用できるようです

4. Delphiで、uses JPEG, GIFImg, PNGImage ; //使用する画像を宣言

Delphi 10.2でテストしています。
image.png


以下サンプルコード

画像データの追加のサンプルコード

FD_MySQL57_40_b_Unit.pas
procedure TForm1.SpeedButton6Click(Sender: TObject);
var
  SQLstmt:String;
begin
  with FDConnection1 do
  try
    SQLstmt :=' INSERT INTO '
             +' test_tbl ( ts_no  , tsblob ) '
             +'	VALUES ('
             +' :ts_no , :tsblob '
             + ')';
    FDQuery1.SQL.Clear;
    FDQuery1.SQL.Text:= sqlstmt;
    FDQuery1.ParamByName('ts_no').AsInteger:=StrToInt( Edit_TS_NO.Text );
    FDQuery1.ParamByName('tsblob').LoadFromFile( Edit_Files.Text ,ftBlob);
    FDQuery1.ExecSQL();
    ShowMessage('登録しました');
  finally
    FDQuery1.Close;
  end;
end;

画像データの表示のサンプルコード

FD_MySQL57_40_b_Unit.pas
procedure TForm1.SpeedButton7Click(Sender: TObject);
var
  SQLstmt:String;
begin
  with FDConnection1 do
  try
    SQLstmt := 'SELECT * FROM test_tbl '
            +  'WHERE ts_no = :ts_no';
    FDQuery1.SQL.Clear;
    FDQuery1.SQL.Text:= sqlstmt;
    FDQuery1.ParamByName('ts_no').AsInteger:=StrToInt(Edit_ts_no.Text);
    FDQuery1.Open();
    if not FDQuery1.Eof then
    begin
      Edit_ts_no.Text  :=IntToStr(FDQuery1.FieldByName('ts_no').AsInteger);
      Image1.Picture.Assign( TBLOBField(FDQuery1.FieldByName('tsblob') ) );
    end;
  finally
    FDQuery1.Close;
  end;
end;

テーブルの作成のサンプルコード

MariaDB では、FireDACのドライバー MySQLを使用したため、 テーブル名の大文字、小文字は区別されるようです

FD_MySQL57_40_b_Unit.pas
procedure TForm1.SpeedButton2Click(Sender: TObject);
var
  sqlstmt:string;
begin
です
  sqlstmt:='CREATE TABLE test_tbl '+
  '('+
  'ID         serial,'+
  'TS_NO      INTEGER  not null,'+
  'TS_NAME    TEXT ,'+
  'TS_QTY     DECIMAL(14,2) Default 0 ,'+
  'TSBLOB     BLOB ,'+
  'TSBLOB1    TINYBLOB,'+   //今回使用していませんが以下も使用できるようです
  'TSBLOB2    MEDIUMBLOB,'+
  'TSBLOB3    LONGBLOB,'+
  'primary key(TS_NO)'+
  ')';
  if FDConnection1.Connected=true then
  begin
    try
      FDQuery1.SQL.Clear;
      FDQuery1.SQL.Add(sqlstmt);
      FDQuery1.ExecSQL();
      ShowMessage('CREATE TABLE');
    except
      on E: EFDDBEngineException do
      ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
    end;
  end
  else ShowMessage('ERR  already exists table ');
end;

MySQLとMariaDBは、インストールが久しぶりだったので、環境に手間取り、簡単な接続手順で実行しました。

サンプルは、MySQL5.7.40 32bit版の dll を使用しています。
このサンプルで、
MySQL5.7.40 32bit
MySQL8.0.31 64bit
MariaDB10.3.37 64bit のサーバーに接続して、動作確認ができました。

このソースのダウロードは、ここからできます

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0