如何在 Oracle SQL Developer 中编辑 BLOB(包含 JSON)?

发布于 2025-01-03 18:24:20 字数 174 浏览 0 评论 0原文

如何在 Oracle SQL Developer 中编辑 BLOB(包含 JSON 文本)?

我可以打开并查看它们,但我需要外部编辑器来编辑它们吗?任何有关使用什么(即使只是记事本)以及如何进行操作的帮助都将在答案中得到极大的赞赏。

编辑:有问题的 BLOB 包含 JSON 文本。

How do I edit BLOBs (containing JSON text) in Oracle SQL Developer?

I can open and view them, but do I need an external editor to edit them? Any help on what to use, even if just notepad, and how to go about it would be greatly appreciated in the answer.

EDIT: BLOBs in question contain JSON text.

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

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

发布评论

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

评论(2

烛影斜 2025-01-10 18:24:20

如果您在 SQL Developer 3.1(可能还有早期版本)中运行返回 BLOB 的查询,则可以双击您感兴趣的特定 BLOB,系统将提示您尝试将数据发送到外部编辑器或尝试让内置 SQL Developer 显示控件尝试将数据解释为图像或文本。如果您选择文本选项,您的 JSON 数据可能会正确显示。

但是,如果您想更改数据,则必须发出 UPDATE 才能实际设置数据。 SQL Developer 不具有直接编辑 LOB 数据的功能。例如,

UPDATE table_name
   SET column_with_json_data = 
          utl_i18n.string_to_raw( '{"foo": {"id": "1", "value": "2"}}' )
 WHERE primary_key = <<some value>>

将使用数据库字符集编码的新 JSON 数据更新指定行。如果您想以其他字符集存储数据,string_to_raw 采用指定字符集的可选第二个参数。因此,如果您想使用 UTF-8 字符集存储数据,您可以这样做

UPDATE table_name
   SET column_with_json_data = 
          utl_i18n.string_to_raw( '{"foo": {"id": "1", "value": "2"}}', 'AL32UTF8' )
 WHERE primary_key = <<some value>>

当然,由于 JSON 数据是文本的,因此最好将数据存储在 CLOB 中,该 CLOB 旨在存储字符大型物体。然后,SQL Developer(和其他工具)可以只显示文本,而不需要您选择结果,然后采取其他操作将其转换为文本。而且您不必将数据转换为 RAW 即可更新数据库中的数据。

如果数据太长,string_to_raw 无法处理(这取决于字符集和数据,但只要 RAW 数据超过 2000 字节就会发生),您可以存储CLOB 中的数据,然后将其转换为用于更新表的 BLOB。这有点复杂,但更灵活。在此示例中,我使用“*”将 JSON 数据填充到 3200 个字符——显然测试数据不再是有效的 JSON,但这对于本问题而言并不重要。

declare
  l_blob        blob;
  l_clob        clob := rpad('{"foo": {"id": "1", "value": "2", "name": "bob"}}',3200,'*');
  l_amt         integer := dbms_lob.lobmaxsize;
  l_dest_offset integer := 1;
  l_src_offset  integer := 1;
  l_csid        integer := dbms_lob.default_csid;
  l_ctx         integer := dbms_lob.default_lang_ctx;
  l_warn        integer;
begin
  dbms_lob.createTemporary( l_blob, false );
  dbms_lob.convertToBlob( l_blob,
                          l_clob,
                          l_amt,
                          l_dest_offset,
                          l_src_offset,
                          l_csid,
                          l_ctx,
                          l_warn );

  -- You'll want to add a WHERE clause as well
  update json_data
     set data = l_blob;

  dbms_lob.freeTemporary( l_blob );
end;
/

If you run a query in SQL Developer 3.1 (and probably earlier releases) that returns a BLOB, you can double-click on the particular BLOB you're interested in where you'll be prompted either to try to send the data to an external editor or to try to have the built-in SQL Developer display control attempt to interpret the data as an image or as text. Your JSON data will probably display correctly if you choose the text option.

If you want to change the data, however, you're going to have to issue an UPDATE to actually set the data. SQL Developer doesn't have the functionality to directly edit the LOB data. For example

UPDATE table_name
   SET column_with_json_data = 
          utl_i18n.string_to_raw( '{"foo": {"id": "1", "value": "2"}}' )
 WHERE primary_key = <<some value>>

will update the specified row with the new JSON data encoded using the database character set. If you want to store the data in some other character set, string_to_raw takes an optional second parameter that specifies the character set. So if you want to store the data using the UTF-8 character set, you'd do something like this

UPDATE table_name
   SET column_with_json_data = 
          utl_i18n.string_to_raw( '{"foo": {"id": "1", "value": "2"}}', 'AL32UTF8' )
 WHERE primary_key = <<some value>>

Of course, since JSON data is textual, you'd be far better off storing the data in a CLOB which is designed to store character large objects. Then SQL Developer (and other tools) could just display the text rather than requiring you to select the result and then take additional actions to convert it to text. And you wouldn't have to convert the data to RAW in order to update the data in the database.

If the data is too long for string_to_raw to handle (which depends on the character set and the data but will occur any time the RAW data exceeds 2000 bytes), you can store the data in a CLOB and then convert that into a BLOB that you use to update the table. That's a bit more complex but it is more flexible. In this example, I'm padding the JSON data out to 3200 characters with a '*'-- obviously the test data is no longer valid JSON but that's not important for purposes of this question.

declare
  l_blob        blob;
  l_clob        clob := rpad('{"foo": {"id": "1", "value": "2", "name": "bob"}}',3200,'*');
  l_amt         integer := dbms_lob.lobmaxsize;
  l_dest_offset integer := 1;
  l_src_offset  integer := 1;
  l_csid        integer := dbms_lob.default_csid;
  l_ctx         integer := dbms_lob.default_lang_ctx;
  l_warn        integer;
begin
  dbms_lob.createTemporary( l_blob, false );
  dbms_lob.convertToBlob( l_blob,
                          l_clob,
                          l_amt,
                          l_dest_offset,
                          l_src_offset,
                          l_csid,
                          l_ctx,
                          l_warn );

  -- You'll want to add a WHERE clause as well
  update json_data
     set data = l_blob;

  dbms_lob.freeTemporary( l_blob );
end;
/
╭⌒浅淡时光〆 2025-01-10 18:24:20

如果您希望在不编写任何 SQL 的情况下更改单个 Blob,则可以在 Oracle SQL Developer 中执行此操作:

  1. 双击显示 (BLOB) 的单元格。编辑按钮(图标为铅笔)应出现在单元格的右侧。单击它。
  2. 点击已保存数据右侧的下载。将其保存在某处。
  3. 在您想要的任何程序中编辑该文件。节省。
  4. 返回 Oracle SQL Developer 的“编辑值”对话框,单击本地数据右侧的加载。找到编辑后保存的文件。现在单击“编辑值”对话框中的确定
  5. 如果您对更改感到满意,请点击提交更改按钮;如果您改变主意,请点击回滚更改按钮。

我认为,如果这只是一个快速的一次性事情,那么比执行整个 dbms_lob.convertToBlob 容易得多。

If you're looking to change individual blobs without having to write any SQL, you can do this in Oracle SQL Developer:

  1. Double click on the cell that says (BLOB). An edit button (pencil for an icon) should appear to the right side of the cell. Click on it.
  2. Click Download, to the right of Saved Data. Save it somewhere.
  3. Edit the file in whatever program you want. Save.
  4. Back in the Edit Value dialog of Oracle SQL Developer, click Load, to the right of Local Data. Find the file wherever you saved it after editing. Click OK in the Edit Value dialog now.
  5. Click the Commit Changes button if you're satisfied with your changes, or the Rollback Changes button if you've changed your mind.

Much easier, I think, than doing the whole dbms_lob.convertToBlob if this is just a quick one-off thing.

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