使用PHP将clob数据导入MySQL TEXT列

时间:2015-03-18 15:22:20

标签: php mysql oracle import

我编写了一个PHP脚本,用于将表从Oracle服务器导入MySQL服务器。导入器工作得很好,除了我在MySQL中创建TEXT的唯一CLOB列在每一行中都显示为NULL。存储在那里的数据只有大量的文本,因此任何需要目标列的二进制文件都不是BLOB。

以下是我在脚本中使用的相关代码:

while ($row = oci_fetch_row($oracle_result)) {  
      // Begin looping through all fields in current row
      for($i=0;$i<$num_fields;$i++) {               
          $this_field = $row[$i];       // Get data value for this field
          // $arFieldDefs holds field metadata
          $field_type = $arFieldDefs[$i]['type'];   // Get data type for this field
          $field_name = $arFieldDefs[$i]['name'];   // Get field name for this field

          if (is_null($this_field)) {
            //This field is NULL
            $this_field = "NULL";
          } else {
            // Field is not null, process its data type
            switch ($field_type) {
                case "NUMBER":
                    //str_replace(",", "", $this_field);    //Remove commas from number string
                    break;
                case "INT":
                    //str_replace(",", "", $this_field);    //Remove commas from number string
                    break;
                case "CHAR":
                    $this_field = "'" .addslashes($this_field) ."'";
                    break;
                case "VARCHAR2":
                    $this_field = "'" . addslashes($this_field) . "'";
                    break;
                case "DATE":
                    $this_field = "'" . $this_field . "'";
                    break;
                case "CLOB":
                    // Meaningful var name to remind us we now have an object of PHP class OCI-Lob
                    $clob_obj = $this_field;
                    // Tried read($clob_obj->size()) instead of load()
                    //   but result was the same
                    $field_contents = $clob_obj->load();
                    $this_field = "'" . $field_contents . "'";
                    break;
                default:
                    // $rec_num is the currently looped record number
                    echo "Bad Field Type '" .$field_type ."' in Field: '".$this_field ."' RecNum: " .$rec_num ."\n";
                    die;
            }
          }
          $value_string .= $this_field . ", ";      // Add comma and space after field value
      } // End FOR loop 
// Cleanup and finish:
// Strip trailing comma+space, put $value_string in parentheses,
// And loop back to build another row in the INSERT
} // End WHILE loop

我认为我不得不使用LONGTEXT,但TEXT应该足够了,因为该列中的最大字符数大约是10,000。

MySQL版本是5.5.3,而Oracle版本是11.2.0.3.0。

UPDATE - 这是一些示例插入,用于显示处理的实际数据。最后一列是目标表上的MEDIUMTEXT的CLOB值:     ('00001206023', 'Notifications: SMB', NULL, NULL, 'SM001331700', 'XMLCSG', 1426176946, 'XMLCSG', NULL, 1426176946, '2015-03-12 16:15:47', 0, '28'), ('00001205439', 'Customer Experience', 'Customers will lose access to all services', NULL, 'CM001176996', 'tstins00', 1426176944, 'tstins00', NULL, 1426160634, '2015-03-12 16:15:46', 0, 'Customers will lose access to all services'),('00001206059', 'Required Tools', 'CustomerCenter-NorthDivision-Video', NULL, 'CM001177349', 'blassi001', 1426176976, 'blassi001', NULL, 1426176976, '2015-03-12 16:16:17', 0, 'CustomerCenter-NorthDivision-Video')

列如下:

  1. entry_id VARCHAR
  2. element_name VARCHAR
  3. short_data VARCHAR
  4. element_data_old VARCHAR(4000)
  5. sm_id VARCHAR
  6. submitter VARCHAR
  7. modified_date INT
  8. last_modified_by VARCHAR
  9. sort_order INT
  10. create_date INT
  11. replicated_date DATETIME
  12. 输入INT
  13. element_data MEDIUMTEXT [以前是BLOB,然后是LONGTEXT]
  14. 创建表语句:

    CREATE TABLE MY_TABLE_NAME (
      ENTRY_ID VARCHAR(11) NOT NULL,
      ELEMENT_NAME VARCHAR(155) DEFAULT NULL,
      SHORT_DATA VARCHAR(255) DEFAULT NULL,
      ELEMENT_DATA_OLD VARCHAR(4000) DEFAULT NULL,
      SM_ID VARCHAR(11) DEFAULT NULL,
      SUBMITTER VARCHAR(254) DEFAULT NULL,
      MODIFIED_DATE INT(11) DEFAULT NULL,
      LAST_MODIFIED_BY VARCHAR(254) DEFAULT NULL,
      SORT_ORDER INT(11) DEFAULT NULL,
      CREATE_DATE INT(11) DEFAULT NULL,
      REPLICATED_DATE DATETIME DEFAULT NULL,
      TYPE INT(11) DEFAULT NULL,
      ELEMENT_DATA MEDIUMTEXT,
      PRIMARY KEY (ENTRY_ID),
      UNIQUE KEY MTN_ENTRYID_PK (ENTRY_ID),
    ) ENGINE=INNODB DEFAULT CHARSET=utf8`
    

0 个答案:

没有答案