这个问题已经在这里有一个答案:
- 代表的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表的小数位数的号码,如果有什么是数据类型,我应该使用? 请指教。
提前致谢!
您可以存储的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;
尝试存储值作为一个字符串,并使用引号:
CREATE TABLE ipdetails( ip_address varchar(15) NOT NULL, vlan_id varchar(50) );
然后 。 。 。
INSERT INTO ipdetails VALUES ('192.169.698.365', 'Sample1')
你可能想每个组件存储在一个单独的字段。