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

时间:2012-01-11 12:42:20

标签: oracle oracle10g clob

我有像(7A + 5dfAA7D ...)字母和数字这样的数据。其长度大于two lack个字符。所以我无法使用update更新该clob列,因为它会抛出一个错误,说明字符串文字太长了。

如何更新值?

请帮帮我。

提前致谢!

2 个答案:

答案 0 :(得分:0)

以下是将数据从文件插入CLOB列的一些Java代码的一部分。诀窍是首先插入一个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

答案 1 :(得分:0)

您可以使用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/PASS@dbserver.mycompany.com control=control.txt
sqlldr USER/PASS@dbserver.mycompany.com 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

此配置源自此示例:Ask Tom