如何使用sql oracle用批量数据更新CLOB列?

发布于 2024-12-26 07:10:49 字数 233 浏览 2 评论 0原文

我有 (7A+5dfAA7D...) 字母和数字等数据。其长度大于缺少两个个字符。因此,我无法使用 update 更新该 clob 列,因为它会抛出一个错误,指出字符串文字太长。

如何更新数值?

请帮我。

提前致谢!

I have the data like (7A+5dfAA7D...) alphabet and numbers. Its length is greater than two lack characters. So I can't update that clob column using update because it throws an error saying the string literal too long.

How to update the values?

please help me.

Thanks in advance!

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

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

发布评论

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

评论(2

涙—继续流 2025-01-02 07:10:49

下面是一些 Java 代码的一部分,它将文件中的数据插入到 CLOB 列中。技巧是首先插入一个empty_clob()值,然后更新记录。

try {
            /* Register the Oracle driver */
            DriverManager.registerDriver(new OracleDriver());

            /* Establish a connection to the Oracle database. I have used the Oracle Thin driver.
              jdbc:oracle:thin@host:port:sid, "user name", "password" */

            conn = DriverManager.getConnection(connectString, userName, passWord);

            /* Set auto commit to false, it helps to speed up things, by default JDBC's auto commit feature is on.
               This means that each SQL statement is commited as it is executed. */

            conn.setAutoCommit(false);
            stmt = conn.createStatement();

            /* Insert all the data, for the BLOB column we use the function empty_blob(), which creates a locator for the BLOB datatype.
               A locator is an object that ponts to the actual location of the BLOB data in the database. A locator is essential to manipulate BLOB data. */

            query = "INSERT INTO CLOB_TABLE (id,data_clob,bestandsnaam,dt_geplaatst) " +
                    "VALUES(" + ID + ", empty_clob(),\'" + fileName + "\',sysdate)";

            //System.out.println(query);

            stmt.execute(query);

            /* Once the locator has been inserted, we retrieve the locator by executing a SELECT statement 
               with the FOR UPDATE clause to manually lock the row. */

            query = "SELECT DATA_CLOB FROM CLOB_TABLE WHERE ID=\'" + ID + "\' FOR UPDATE";

            //System.out.println(query);
            rs = stmt.executeQuery(query);
            //System.out.println("Select statement uitgevoerd");

            if (rs.next()) {

                /* Once a locator has been retrieved we can use it to insert the binary data into the database. */
                CLOB clob = (CLOB)((OracleResultSet)rs).getClob(1);
                os = clob.getAsciiOutputStream();
                final File f = new File(fileName);
                is = new FileInputStream(f);
                final byte[] buffer = new byte[clob.getBufferSize()];
                int bytesRead = 0;
                while ((bytesRead = is.read(buffer)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                clob = null;
                returnValue = 0;
            }


        } catch

Here is part of some java code that inserts data from a file into a CLOB column. The trick is to first insert an empty_clob() value and then to update the record.

try {
            /* Register the Oracle driver */
            DriverManager.registerDriver(new OracleDriver());

            /* Establish a connection to the Oracle database. I have used the Oracle Thin driver.
              jdbc:oracle:thin@host:port:sid, "user name", "password" */

            conn = DriverManager.getConnection(connectString, userName, passWord);

            /* Set auto commit to false, it helps to speed up things, by default JDBC's auto commit feature is on.
               This means that each SQL statement is commited as it is executed. */

            conn.setAutoCommit(false);
            stmt = conn.createStatement();

            /* Insert all the data, for the BLOB column we use the function empty_blob(), which creates a locator for the BLOB datatype.
               A locator is an object that ponts to the actual location of the BLOB data in the database. A locator is essential to manipulate BLOB data. */

            query = "INSERT INTO CLOB_TABLE (id,data_clob,bestandsnaam,dt_geplaatst) " +
                    "VALUES(" + ID + ", empty_clob(),\'" + fileName + "\',sysdate)";

            //System.out.println(query);

            stmt.execute(query);

            /* Once the locator has been inserted, we retrieve the locator by executing a SELECT statement 
               with the FOR UPDATE clause to manually lock the row. */

            query = "SELECT DATA_CLOB FROM CLOB_TABLE WHERE ID=\'" + ID + "\' FOR UPDATE";

            //System.out.println(query);
            rs = stmt.executeQuery(query);
            //System.out.println("Select statement uitgevoerd");

            if (rs.next()) {

                /* Once a locator has been retrieved we can use it to insert the binary data into the database. */
                CLOB clob = (CLOB)((OracleResultSet)rs).getClob(1);
                os = clob.getAsciiOutputStream();
                final File f = new File(fileName);
                is = new FileInputStream(f);
                final byte[] buffer = new byte[clob.getBufferSize()];
                int bytesRead = 0;
                while ((bytesRead = is.read(buffer)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                clob = null;
                returnValue = 0;
            }


        } catch
等待我真够勒 2025-01-02 07:10:49

您可以使用 SQL*Loader 加载数据,但必须从文件中执行。尽管您可能会编写一个脚本来设置包含数据的顺序文件,但这可能会或可能不会困难,具体取决于涉及的数据量。

对于名为 TEST 的表,其中包含 TEST_ID 和 TEST_BLOB 列:

Control.dat:

load data
infile data.dat
replace 
into table TEST
Fields terminated by ','
(
  TEST_ID, 
   lob_file FILLER CHAR,
  TEST_CLOB LOBFILE(lob_file) TERMINATED BY EOF
)

data.dat:

1,c:\work\clob1_data.dat
2,c:\work\clob2_data.dat
etc...

C:\Work>sqlldr USER/[email protected] control=control.txt
sqlldr USER/[email protected] control=control.txt
SQL*Loader: Release 10.2.0.1.0 - Production on Thu Sep 24 01:20:06 2009
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
Commit point reached - logical record count 2

此配置源自以下示例: 问汤姆

You can use SQL*Loader to load the data, but you have to do it from a file. This may or may not be difficult depending on how much data is involved, though you could likely write a script which sets up sequential files containing your data.

For a table called TEST, with columns of TEST_ID and TEST_BLOB:

Control.dat:

load data
infile data.dat
replace 
into table TEST
Fields terminated by ','
(
  TEST_ID, 
   lob_file FILLER CHAR,
  TEST_CLOB LOBFILE(lob_file) TERMINATED BY EOF
)

data.dat:

1,c:\work\clob1_data.dat
2,c:\work\clob2_data.dat
etc...

C:\Work>sqlldr USER/[email protected] control=control.txt
sqlldr USER/[email protected] control=control.txt
SQL*Loader: Release 10.2.0.1.0 - Production on Thu Sep 24 01:20:06 2009
Copyright (c) 1982, 2005, Oracle.  All rights reserved.
Commit point reached - logical record count 2

This configuration was derived from this example: Ask Tom

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