使用 sql 将值存储在 TBlobField 中

发布于 2024-08-17 20:50:53 字数 116 浏览 3 评论 0原文

我想使用 SQL 命令将图像存储到数据库中,我知道使用 TBlobField.LoadFromFile 等其他方法,但我们使用自己的 sql 命令来更新数据库,这就是我需要这样做的原因。

我该怎么做呢?

I want to store images into a database using SQL commands, I know other ways using TBlobField.LoadFromFile etc, but we make our own sql commands to update the database that's why I need to do this.

How should I go about doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

拿命拼未来 2024-08-24 20:50:53

我从未尝试过这个(目前不在办公桌旁),但是参数有效吗?例如:

Query.Sql.Clear;
Query.Sql.Add('update myTable set myField = :blobVal where myId = :idVal');
Query.ParamByName('idVal').AsInteger := SomeId;
Query.ParamByName('blobVal').LoadFromFile(....
//or
Query.ParamByName('blobVal').LoadFromStream(....
Query.ExecSql;

这允许您使用 SQL(而不是 .Edit 等方法)并仍然插入 blob 数据

I've never tried this (and away from desk at the moment), but would parameters work? eg:

Query.Sql.Clear;
Query.Sql.Add('update myTable set myField = :blobVal where myId = :idVal');
Query.ParamByName('idVal').AsInteger := SomeId;
Query.ParamByName('blobVal').LoadFromFile(....
//or
Query.ParamByName('blobVal').LoadFromStream(....
Query.ExecSql;

This allows you to use SQL (rather than the .Edit, etc methods) and still insert blob data

泛滥成性 2024-08-24 20:50:53

如果我理解正确的话,您有某种 SQL 生成系统而不是使用数据集,并且您想知道如何生成 SQL 来正确对 Blob 字段执行 INSERT 或 UPDATE。

您需要的是一种将图像序列化为字符串的方法。例如:

function ImageToString(image: TImage): string;
var
  stream: TStringStream;
begin
  stream := TStringStream.Create;
  try
    image.SaveToStream(stream);
    result := image.DataString;
  finally
    stream.Free;
  end;
end;

准备就绪后,在 SQL 中添加一个标记,例如 set IMAGE_FIELD = $IMAGE$,然后使用 StringReplace 将 $IMAGE$ 标记替换为图像的字符串表示形式。

或者,如果您正在使用的系统支持参数,您也可以使用参数,就像 Graza 建议的那样。

If I understand correctly, you have some sort of SQL-generation system instead of using datasets, and you want to know how to generate the SQL to do an INSERT or UPDATE to a Blob field correctly.

What you'll need is a way to serialize your image to a string. For example:

function ImageToString(image: TImage): string;
var
  stream: TStringStream;
begin
  stream := TStringStream.Create;
  try
    image.SaveToStream(stream);
    result := image.DataString;
  finally
    stream.Free;
  end;
end;

Once that's ready, put a token in your SQL, something like set IMAGE_FIELD = $IMAGE$, and then use StringReplace to replace the $IMAGE$ token with the string representation of your image.

Or you could use parameters, like Graza suggested, if the system you're working with supports them.

友谊不毕业 2024-08-24 20:50:53

试试这个:

Query.Sql.Clear;
Query.Sql.Add('update myTable set myField = :blobVal where myId = :idVal');
Query.ParamByName('idVal').AsInteger := SomeId;
Query.Params.CreateParam(ftBlob,'blobVal',ptInput).LoadFromFile('c:\path.png',ftGraphic);
Query.ExecSql;

Try this:

Query.Sql.Clear;
Query.Sql.Add('update myTable set myField = :blobVal where myId = :idVal');
Query.ParamByName('idVal').AsInteger := SomeId;
Query.Params.CreateParam(ftBlob,'blobVal',ptInput).LoadFromFile('c:\path.png',ftGraphic);
Query.ExecSql;
丿*梦醉红颜 2024-08-24 20:50:53
jpg := TJPEGImage.Create;
jpg.Assign(Image1.Picture.Graphic);
strm := TMemoryStream.Create;
strm.Position:= 0;
jpg.SaveToStream(strm);
IBSQL1.Close;
IBSQL1.SQL.Clear;
IBSQL1.SQL.Add('INSERT INTO ENTRY(FORMNUM, JPG) VALUES(');
IBSQL1.SQL.Add(    quotedstr(edtFormNum.Text));
IBSQL1.SQL.Add(',  :JPG');
IBSQL1.SQL.Add(')');
IBSQL1.Params.ByName('JPG').LoadFromStream(strm);
IBSQL1.ExecQuery;
strm.Free;
jpg.Free;
jpg := TJPEGImage.Create;
jpg.Assign(Image1.Picture.Graphic);
strm := TMemoryStream.Create;
strm.Position:= 0;
jpg.SaveToStream(strm);
IBSQL1.Close;
IBSQL1.SQL.Clear;
IBSQL1.SQL.Add('INSERT INTO ENTRY(FORMNUM, JPG) VALUES(');
IBSQL1.SQL.Add(    quotedstr(edtFormNum.Text));
IBSQL1.SQL.Add(',  :JPG');
IBSQL1.SQL.Add(')');
IBSQL1.Params.ByName('JPG').LoadFromStream(strm);
IBSQL1.ExecQuery;
strm.Free;
jpg.Free;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文