如何在 delphi / interbase 中使用字符串作为 blob 进行插入查询

发布于 2024-10-23 16:10:34 字数 448 浏览 9 评论 0原文

我想做一个查询插入:

INSERT INTO A_TABLE (BLOB_FIELD) VALUES(MY_BLOB_VAL)

但我在 delphi 中只有字符串值,例如:

procedure INSERT_BLOB_QUERY
  var
    query:String;
    my_blob_val:String;
begin
   my_blob_val := 'a blob string to be inserted';
   query := 'INSERT INTO A_TABLE (BLOB_FIELD) VALUES(' + my_blob_val + ')';
   // to execute a query....
end;

出现的问题是字符串到 blob 的转换。

那么如何在 interbase blob 字段中插入字符串???

I'd like to make a query insert:

INSERT INTO A_TABLE (BLOB_FIELD) VALUES(MY_BLOB_VAL)

but I have only string values in delphi for ex:

procedure INSERT_BLOB_QUERY
  var
    query:String;
    my_blob_val:String;
begin
   my_blob_val := 'a blob string to be inserted';
   query := 'INSERT INTO A_TABLE (BLOB_FIELD) VALUES(' + my_blob_val + ')';
   // to execute a query....
end;

The problem that occours is string to blob conversion.

So how to I insert a string in a interbase blob field???

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

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

发布评论

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

评论(1

抚你发端 2024-10-30 16:10:34

像这样:

procedure INSERT_BLOB_QUERY;
begin
  query.SQL.Text := 'INSERT INTO A_TABLE (BLOB_FIELD) VALUES (:VAL)';
  query.ParamByName('VAL').AsString := 'a blob string to be inserted';
end;

您的代码不起作用,因为您没有将字符串作为参数传递,而是将其作为查询的一部分传递。如果你这样做,你显然需要引用它:你这样做的方式 Interbase 会尝试将其解释为 SQL 命令,而不是解释为要插入数据库列中的文字字符串。

尽管如此,不要去引用。使用参数总是更好,更安全!

Like this:

procedure INSERT_BLOB_QUERY;
begin
  query.SQL.Text := 'INSERT INTO A_TABLE (BLOB_FIELD) VALUES (:VAL)';
  query.ParamByName('VAL').AsString := 'a blob string to be inserted';
end;

Your code doesn't work because you're not passing the string as a parameter, you're passing it as part of the query. If you do that, you obviously need to QUOTE it: the way you're doing it Interbase will try to interpret it as SQL commands, not as a literal string to be inserted in a db column.

None the less, don't go for quoting. It's always better to use parameters, it's safer!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文