procedure TForm1.Button3Click(Sender: TObject);
var
stmt: PMYSQL_STMT;
bind: PMYSQL_BIND;
affected_rows: my_ulonglong;
param_count: Integer;
InsertQuery:
String;
fs: TFileStream;
data:
array of byte;
filesize: Int64;
begin
Memo.Lines.Add('
mysql_stmt_init()');
stmt := mysql_stmt_init(LibHandle);
if (stmt=nil)
then begin
Memo.Lines.Add('
mysql_stmt_init(), out of memory');
exit;
end;
Memo.Lines.Add('
mysql_stmt_init() done');
InsertQuery := '
INSERT INTO testblob(filedata) VALUES(?)';
Memo.Lines.Add('
mysql_stmt_prepare()');
if (mysql_stmt_prepare(stmt, PAnsiChar(InsertQuery), Length(InsertQuery))<>0)
then begin
Memo.Lines.Add('
mysql_stmt_prepare(), INSERT failed');
Memo.Lines.Add(mysql_stmt_error(stmt));
end;
Memo.Lines.Add('
mysql_stmt_prepare() done');
Memo.Lines.Add('
mysql_stmt_param_count()');
param_count := mysql_stmt_param_count(stmt);
Memo.Lines.Add(Format('
total parameters in INSERT: %d', [param_count]));
if (param_count <> 1)
then begin
Memo.Lines.Add('
invalid parameter count returned by MySQL');
exit;
end;
bind := mysql_bind_init(1);
//** Different to org. Demo: Alloc 3 MYSQL_BIND **
try
fs := TFileStream.Create('
TestDatei1.txt',fmOpenRead);
try
SetLength(data,fs.Size);
fs.ReadBuffer(data[0],fs.Size);
filesize := fs.Size;
finally
fs.Free;
end;
mysql_bind_set_param(bind, 0, MYSQL_TYPE_BLOB, @data[0], filesize, @filesize,
nil);
Memo.Lines.Add('
mysql_stmt_bind_param()');
if mysql_stmt_bind_param(stmt, bind)
then begin
Memo.Lines.Add('
mysql_stmt_bind_param() failed');
Memo.Lines.Add(mysql_stmt_error(stmt));
exit;
end;
Memo.Lines.Add('
mysql_stmt_bind_param() done');
Memo.Lines.Add('
mysql_stmt_execute()');
if (mysql_stmt_execute(stmt)<>0)
then begin
Memo.Lines.Add('
mysql_stmt_execute() failed');
Memo.Lines.Add(mysql_stmt_error(stmt));
exit;
end;
Memo.Lines.Add('
mysql_stmt_execute() done');
affected_rows := mysql_stmt_affected_rows(stmt);
Memo.Lines.Add(Format('
total affected rows(insert 2): %u', [affected_rows]));
if (affected_rows <> 1)
then begin
Memo.Lines.Add('
invalid affected rows by MySQL');
exit;
end;
finally
FreeMem(bind);
end;
if mysql_stmt_close(stmt)
then begin
Memo.Lines.Add('
failed while closing the statement');
Memo.Lines.Add(mysql_stmt_error(stmt));
exit;
end;
end;