简体   繁体   English

如何将十六进制数据插入到oracle表中?

[英]How to insert Hex data into the oracle table?

I have the following table: 我有下表:

create table o_newstdata1 (
field1 raw(8)
);

the following input.data file: 以下input.data文件:

0x12345678
0x1234
0x12

how can I insert such a data into my o_newstdata1 table? 如何将这样的数据插入我的o_newstdata1表?

I've tried to load it as a varchar data: 我试图将其加载为varchar数据:

load data
INFILE '/export/home/mine/input.data'
replace
into table o_newstdata1
trailing nullcols
(
field1 varchar (8)
)

And queried something like this: 并查询如下内容:

select field1 from o_newstdata1 where utl_raw.cast_to_raw(field1) like '0x1234';

but it doesn't work, any suggestions? 但这不起作用,有什么建议吗?

There are possibly two sources of errors: 错误可能有两个来源:

  1. Loading raw data with SQL*Loader 使用SQL * Loader加载原始数据
  2. Querying the raw data 查询原始数据

To check if the querying part works, use something like: 要检查查询部分是否工作,请使用类似以下内容的方法:

CREATE TABLE o_newstdata1 (field1 RAW(8)); 
INSERT INTO o_newstdata1(field1) VALUES ('12AB');
INSERT INTO o_newstdata1(field1) VALUES ('34EF');

You don't need to cast to a raw, you can use Oracle's hex format directly. 您无需转换为原始格式,可以直接使用Oracle的十六进制格式。 Version 1 uses implicit conversion from RAW TO VARCHAR2 , version 2 explicit conversion: 版本1使用从RAWVARCHAR2隐式转换,版本2使用显式转换:

SELECT * FROM o_newstdata1 WHERE field1 = '12AB';
SELECT * FROM o_newstdata1 WHERE rawtohex(field1)='12AB'; 

Should the datatype not be specified as RAW? 数据类型不应该指定为RAW吗?

From the docs : 文档

When raw, binary data is loaded "as is" into a RAW database column, it is not converted by the Oracle database. 将原始二进制数据“按原样”加载到RAW数据库列中时,Oracle数据库不会对其进行转换。 If it is loaded into a CHAR column, then the Oracle database converts it to hexadecimal. 如果将其加载到CHAR列中,则Oracle数据库会将其转换为十六进制。 It cannot be loaded into a DATE or number column. 无法将其加载到DATE或number列中。

The length of this field is the number of bytes specified in the control file. 该字段的长度是控制文件中指定的字节数。 This length is limited only by the length of the target column in the database and by memory resources. 此长度仅受数据库中目标列的长度和内存资源的限制。 The length is always in bytes, even if character-length semantics are used for the data file. 即使字符长度语义用于数据文件,长度也始终以字节为单位。 RAW data fields cannot be delimited. RAW数据字段无法定界。

If your data is not really binary/raw (as indicated by the size of the column), but just a number, you could store it as a number. 如果您的数据不是真正的二进制/原始(如列的大小所示),而是一个数字,则可以将其存储为数字。

Hex is just one way to represent a number (a notation). 十六进制只是表示数字(表示法)的一种方法。 So I would just store it as a numeric, and put a view on top of it, so you can see/query hex values directly. 因此,我只将其存储为数字,然后在其上方放置一个视图 ,以便您可以直接查看/查询十六进制值。

This way you could even add or compare these values in the database. 这样,您甚至可以在数据库中添加或比较这些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM