在甲骨文的SQL表中存储的IP地址[复制](Storing an IP Address in a O

2019-08-17 02:36发布

这个问题已经在这里有一个答案:

  • 代表的IPv4 / IPv6地址在甲骨文 7个回答

我创建了一个表,用于存储IP地址。

CREATE TABLE ipdetails( ip_address DECIMAL(16,4) NOT NULL, vlan_id varchar(50) );

但是,当我尝试插入到表中它给了我一个错误:

INSERT INTO ipdetails VALUES (192.169.165.128, 'Sample1')

反正我有可以存储与很多SQL表的小数位数的号码,如果有什么是数据类型,我应该使用? 请指教。

提前致谢!

Answer 1:

您有几种选择:

  • 它存储为VARCHAR。 IP地址是不是真的了一些你可能会做数学,所以为什么它存储在数字格式?
  • 在十六进制保存它作为一个单一的数字。 虽然你不能真正做到这一点十进制,十六进制中的IP是一个8位数字。
  • 它存储为(最多)四个独立的领域。 也许没有必要,但是对于某些应用(如您可能希望只,主要与IP的一个组成部分予以关注),这可能是有用的。


Answer 2:

您可以存储的IP为一个数字,这是他们内部是如何存在的,但你必须编写代码来来回转换:

#include <arpa/inet.h>
/* 
 * Returns a pointer to an internal array containing the string, which is overwritten with each call to the function.
 * You need to copy the string if you want to keep the value returned.
 */
extern char *inet_ntoa (struct in_addr in);

/* 
 * Convert Internet host address from numbers-and-dots notation in CP
 * into binary data and store the result in the structure inp.
 */
extern int inet_aton (const char *cp, struct in_addr *inp);

下面是一些简单的SQL是做什么的其中之一做IP->小数,使用127.0.0.1

SELECT
TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,1))*POWER(2,24)
+ TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,2))*POWER(2,16)
+ TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,3))*POWER(2,8)
+ TO_NUMBER(REGEXP_SUBSTR('127.0.0.1','\w+',1,4))*POWER(2,0) IP
FROM
DUAL;


Answer 3:

尝试存储值作为一个字符串,并使用引号:

CREATE TABLE ipdetails( ip_address varchar(15) NOT NULL, vlan_id varchar(50) );

然后 。 。 。

INSERT INTO ipdetails VALUES ('192.169.698.365', 'Sample1')

你可能想每个组件存储在一个单独的字段。



文章来源: Storing an IP Address in a Oracle SQL Table [duplicate]