I am trying to insert random values into a table from my linux terminal, but when i use the following SQL statement,
INSERT INTO kCreate (k1 , k2) VALUES ('$RANDOM' , '$RANDOM');
where k1 and k2 are of datatype INT, 0 is being inserted instead of a random value, What am i doing wrong here ?
k1 and k2 are of INT type, no need to put the value inside single quote, try this:
sql="INSERT into kcreate ( k1, k2) values ($RANDOM, $RANDOM);"
echo $sql | mysql -ppassword test
You can use rand() function of mysql with ceil(). Here is an example.
INSERT INTO kCreate (k1 , k2)
VALUES (ceil(rand()*1000) , ceil(rand()*1000));
Related
I have these 2 variables here
name := request.FormValue("username")
pass := request.FormValue("password")
I want to insert those 2 variables into my database
db.Query("INSERT INTO `godb` (`Username`, `Password`) VALUES ( )")
I tried (name,pass) ('name','pass') ($name, $pass) , none of them work.
Hope the question is not stupid, but I've been looking for solutions online but I did't understand them. Thanks !
From Using Prepared Statements
Parameter Placeholder Syntax
The syntax for placeholder parameters in prepared statements is
database-specific. For example, comparing MySQL, PostgreSQL, and
Oracle:
MySQL PostgreSQL Oracle
===== ========== ======
WHERE col = ? WHERE col = $1 WHERE col = :col
VALUES(?, ?, ?) VALUES($1, $2, $3) VALUES(:val1, :val2, :val3)
You tried PostgreSQL syntax but you use MySQL.
query should be in this format
db.Query("INSERT INTO table ($1, $2) VALUES (column1, column2)", value1, value2)
in your case something like that
db.Query("INSERT INTO godb ($1, $2) VALUES (username, password)", name, pass)
I am trying to use the coalesce function in SQL to avoid getting an error when inserting a row with an auto-incrementing uid into a table that is null. However, the following code is still giving me:
"cannot insert the value null into column 'TABLE_ONE_UID'".
cmdEx.ExecuteNonQuery(
"INSERT INTO TABLE_ONE
(TABLE_ONE_UID, USER_UID, SHT_DATE,
C_S_UID, CST_DATE,
CET_DATE, S_M, PGS)
VALUES ((SELECT MAX(COALESCE(TABLE_ONE_UID, 0)) + 1
FROM TABLE_ONE),
127, '2009-06-15T13:45:30',
0, '2009-06-15T13:45:30','2010-06-15T13:45:30',
'TEST DELETE THIS ROW', 0 )");
The correct way to solve this is with an auto_increment column:
create table PMS_CALC_SCHEDULE (
PMS_CALC_SCHEDULE_UID int auto_increment primary key,
. . .
);
If, for some reason, you want to do the calculation yourself, subject your code to race conditions, and have slower inserts, then you need to do the coalesce in the right place:
INSERT INTO PMS_CALC_SCHEDULE (PMS_CALC_SCHEDULE_UID, . . .)
SELECT COALESCE(MAX(PMS_CALC_SCHEDULE_UID), 0) + 1,
. . .
FROM PMS_CALC_SCHEDULE ;
This would happen when your source table doesn't have any row, MAX would return null in this case.
To prevent this, you can use interchange COALESCE and MAX, e.g.:
INSERT INTO PMS_CALC_SCHEDULE
(PMS_CALC_SCHEDULE_UID, USER_UID, SCHEDULED_DATE,
PMS_CALC_STATUS_UID, CALCULATION_START_DATE,
CALCULATION_END_DATE, STATUS_MESSAGE, PROGRESS)
VALUES ((SELECT COALESCE(MAX(PMS_CALC_SCHEDULE_UID), 0) + 1
FROM PMS_CALC_SCHEDULE),
127, '2009-06-15T13:45:30',
0, '2009-06-15T13:45:30','2010-06-15T13:45:30',
'TEST DELETE THIS ROW', 0 )")
Here's the SQL Fiddle.
If the field is set to AutoIncrement on the table itself, just leave the field out of your Insert-Statement. The value is added by the database itself.
I am trying to insert values coming from a select and variable :
INSERT INTO routeur (`codeAdherent`, `quantiteArticle`, `dateFin`) VALUES
(SELECT `codeAdherent` FROM adherents WHERE categorie = 'G', `quantiteArticle` = $a, `dateFin`= $b);
Write it with and without VALUES, with and without IN, with and without brackets but I always get an synthax error.
Where is my mistake?
Try below:
INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin)
SELECT codeAdherent, #a, #b FROM adherents WHERE categorie = 'G'
You have to read carefully the INSERT syntax because you have got many errors.
This is the right syntax:
INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin)
SELECT codeAdherent, '$a', '$b'
FROM adherents
WHERE categorie = 'G'
PS: To avoid the SQL Injection you should use Prepared Statements
You can try this out :
INSERT INTO routeur (codeAdherent, quantiteArticle, dateFin) VALUES
(SELECT codeAdherent FROM adherents WHERE categorie = 'G', $a, $b);
I have an encrypted column in mysql . I need to replace a substring in it .
If it was not encrypted then I would have used
UPDATE my_table
SET my_field = REPLACE(my_field, 'olddata', 'newdata')
If it was entire column updation , I would use
UPDATE my_table
SET my_field = AES_ENCRYPT('newdata' , 'KEY')
where AES_DECRYPT(my_field , 'KEY') = 'olddata'
But how do I use both the above codes together ? REPLACE with AES_ENCRYPT ?
You'd need to:
decrypt
replace
encrypt again
UPDATE my_table
SET my_field = AES_ENCRYPT(REPLACE(AES_DECRYPT(my_field , 'KEY'), 'olddata', 'new data'), 'KEY')
WHERE AES_DECRYPT(my_field , 'KEY') LIKE '%olddata%'
Here is dbfiddle demo
I have a field "my_data" with strings like these :
XX-12-XXXX
12-XX-2000
XX-XX-2000
XX-XX-XXXX
and I'd like to change every XX and XXXX with, respectively, 00 and 0000
How can I do it with a MySql query?
Use the REPLACE function:
UPDATE myTable
SET my_data = REPLACE(my_date, 'XX', '00')
Note:
I am using XX as this will also replace XXXX, but not single occurrences of X.
You cam use REPLACE() function:
UPDATE table
SET my_data = REPLACE(my_data, 'X', '0');
SELECT REPLACE(my_data, 'X', '0') AS myData
FROM table;