Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL SHA256 with Insert Statement

I want to create a script to fill a database for testing. How would I set a string to be hashed and the inserted into the database?

I have:

INSERT INTO `loop`.`User`
(`userID`,
`firstName`,
`lastName`,
`email`,
`password`,
`userName`,
`bio`,
`spamCount`)
VALUES
('gZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL',
'Joe',
'Smith',
'[email protected]',
SHA2('testgZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL', 256),
'[email protected]',
"TEST BIO",
0);

How do I hash the string and INSERT in same statement?

like image 717
John Down Avatar asked Jan 11 '16 01:01

John Down


1 Answers

You can insert a SELECT instead of VALUES to run a function on one of the inputs:

INSERT INTO `loop`.`User`
(`userID`,
 `firstName`,
 `lastName`,
 `email`,
 `password`,
 `userName`,
 `bio`,
 `spamCount`)
SELECT
'gZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL',
'Joe',
'Smith',
'[email protected]',
SHA2('testgZvTtlPtjGRqeMBaLji3HxoKB5EZCsNL', 256),
'[email protected]',
"TEST BIO",
0;
like image 125
wogsland Avatar answered Nov 06 '22 11:11

wogsland