I created table like this
create table my(username varchar(23),
rolnumber varchar(12),
pword varchar(50));
Trying to insert values like this
insert into my
values ('sandeep','10r81a0229',aes_encrypt('ori12','sand12'));
getting error:
1366 incorrect string value
AES_ENCRYPT() returns binary data which can't be stored in VARCHAR field. You should use appropriate field type, like blob, instead.
From manual:
if you want to store binary values such as results from an encryption or compression function that might contain arbitrary byte values, use a BLOB column rather than a CHAR or VARCHAR column
Related
I am trying to populate a MySQL database field with a binary(16) value of an IP address and I'm getting:
Warning: #1265 Data truncated for column 'IP' at row 1
The binary value I am trying to insert is
00000000000000000000ffff5bd25452
What am I doing wrong?
MySql table schema
Currently you're losing data for using BINARY(16). To insert you need to unhex the value, for example:
INSERT INTO <your-table> (IP) VALUES(UNHEX("00000000000000000000ffff5bd25452"));
To get it back you'll be required to use HEX, to have the original form back:
SELECT HEX(IP) FROM <your-table>;
Edit 1: in apropos of your comment
how can I insert an IP string such as 107.180.58.16
As IPv4 addresses are 4 byte long, same as an INT (UNSIGNED):
Use INET_ATON to convert it to INT, and INET_NTOA to convert it back to IPv4 - for example:
INSERT INTO <your-table> (IP) VALUES (INET_ATON("107.180.58.16"));
SELECT INET_NTOA(IP) FROM <your-table>;
And, for IPv6 addresses i.e. 128 bits you can use 16 bytes binary field, BINARY(16) or VARBINARY(16).
How to store an IP in mySQL
In MySQL, I want to declare a default value to a varbinary column. How do I achieve it ?
DECLARE result varbinary(8000);
SET result = 0x;
I used select CHAR_LENGTH(00101) and it gives me a result 3. I am expecting my result to be 5 (number of characters in string). To measure the length of a varbinary string, how do I do it ?
When you create your table you can specify a default. For binary data you should probably express it as a hex string, 0x... style:
CREATE TABLE binary_default (
id INT PRIMARY KEY AUTO_INCREMENT,
binary_data VARBINARY(8000) DEFAULT 0x010203
);
You can test this works with:
INSERT INTO binary_default VALUES ()
Then fetch, but as it's binary, you might want a hex view:
SELECT id, HEX(binary_data) FROM binary_default
CREATE TABLE GroovyExps_Tgt
(EMPNO SMALLINT,
FIRSTNAME VARCHAR(20) NOT NULL,
MIDINIT CHAR(1) NOT NULL,
LASTNAME VARCHAR(15) NOT NULL,
SALARY_INT INT,
SALARY_Decimal DECIMAL,
SALARY_Numeric NUMERIC,
SALARY_FLOAT FLOAT(9),
SALARY_MONEY MONEY,
SALARY_SMALLMONEY SMALLMONEY,
BIRTHDATE DATE,
HIREDATE_DATETIME DATETIME,
JOIN_TIME TIME,
JOINTIME DATETIME)
insert into GroovyExps_Tgt values(000080,'LEE','B','BRETT',11111111.11111111,
11111111.11111111,11111111.11111111,
11111111.11111111,11111111.11111111,
11111111.11111111,'1985-05-10',
'2014-04-22 20:25:48.002','20:25','2014-04-22 20:25:48.002')
Error:
Msg 8115, Level 16, State 4, Line 1 Arithmetic overflow error
converting numeric to data type smallmoney. The statement has been
terminated.
I need to insert exact data into table without any change in table definition!!!!
As the error message suggests, this error happens when converting a character or string value, whether of char, varchar, nchar or nvarchar data type, into a smallmoney data type and the character value has invalid characters that cannot be converted to smallmoney data type.
As per your insert statement 11111111.11111111 this value you are
using as a smallmoney value. Smallmoney datatype takes 4 bytes to
store the value in range of -214,748.3648 to +214,748.3647. Now you
see you are using large value for smallmoney that's why it's giving
you error.
The problem is that value 11,111,111 exceeds smallmoney's maximum allowed value which is between - 214,748.3648 and 214,748.3647. For more details you can refer to:
http://msdn.microsoft.com/en-us/library/ms179882.aspx
It is not possible to insert that value in that table without changing its definition, using the money data type yo can accomplish it... could you please tell me what you are trying to accomplish there, so I may help you better?
I've found similar questions, but no clear answer for this question. I have this table:
CREATE DATABASE testDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE testTable
(
firstName binary(32) not null,
lastName binary(32) not null
/* Other non-binary fields omitted */
)
engine=INNODB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
This statement executes just fine:
INSERT INTO testTable (firstName) VALUES (AES_ENCRYPT('Testname', 'test'));
But, this returns NULL:
SELECT AES_DECRYPT(firstName, 'test') FROM testTable;
Why does this return NULL?
Fwiw, this returns "testValue" as expected:
SELECT AES_DECRYPT(AES_ENCRYPT('testValue','thekey'), 'thekey');
The answer is that the columns are binary when they should be varbinary. This article explains it:
Because if AES_DECRYPT() detects invalid data or incorrect
padding, it will return NULL.
With binary column types being fixed length, the length of the input value must be known to ensure correct padding. For unknown length values, use varbinary to avoid issues with incorrect padding resulting from differing value lengths.
When you insert binary data into a VARCHAR field there are some binary characters that a VARCHAR can't handle and they will mess up in the inserted value. And then the inserted value will not be the same when you retrieve it.
1.select hex(aes_encrypt(file,'key'));
2.select aes_decrypt(unhex(file),'key');
Check if type of your field is blob instead binary(32)
Did you try different values other than 'Testname'?
Do other values work?
I ask because I had a situation while testing 2 test credit card numbers where one decrypted fine and the other returned null.
The answer was to hex and unhex as suggested by "abhinai raj"
I am using MySQL database.
I have one table having column with datatype binary(16).
I need help with the insert statement for this table.
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9e-efdf-b449');
Error : Lookup Error - MySQL Database Error: Data too long for column 'distid' at row 1
How to resolve this issue?
You should remove the hyphens to make the value match the length of the field...
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9eefdfb449');
Also, MySQL standard is to use this notation to denote the string as binary... X'9fad5e9eefdfb449', i.e.
insert into assignedresource values (X'9fad5e9eefdfb449');
Well, assuming that you want to strictly insert a hexadecimal string, first you need to remove the dashes and then "unhex" your string before inserting it into a binary(16) data type column, the code would go like this:
INSERT INTO `assignedresource` VALUES(UNHEX(REPLACE('9fad5e9e-efdf-b449','-','')));
Also... the "usable" data you are inserting is actually 8 bytes after undashing it, so binary(8) would do fine if you plan on not storing the dashes.
You can strip the hyphens and perpend 0x to the value unquoted, like this:
insert into assignedresource values (0x9fad5e9eefdfb449);
As well as, as this (mentioned in other answers):
insert into assignedresource values (X'9fad5e9eefdfb449');
Both are valid notation for a hexadecimal literal.
Your string is 18 char long, change the database
CREATE TABLE `assignedresource` (
`distid` binary(18) NOT NULL
)