Simple trigger doesn't work - mysql

Hy. I am learning mysql and I have a simple trigger example in my book that doesn't work when I try it
create trigger newproduct after insert on products
for each row select "Product selected";
The error is :
Error Code: 1415. Not allowed to return a result set from a trigger
Why do i get this error? I am using mysql 5.2

Because you should set data to any var like
SET #trigger_var = (select "Product selected");

Related

mysql update with filter by json selection

This sql statement works
SELECT * FROM form_submission WHERE JSON_EXTRACT(json_data, "$.startup") = "test company"
but when use for update
UPDATE form_submission SET status='submit' WHERE JSON_EXTRACT(json_data, "$.startup") = "test company"
will return error of
"Syntax error in JSON text in argument 1 to function 'json_extract' at position 617"
please help
Some functions, including json_extract, result in an error when encountering bad data only when used in a data modification statement such as UPDATE. You can prevent it in the case by checking if json_data is valid before extracting:
UPDATE form_submission SET status='submit' WHERE JSON_VALID(json_data) AND JSON_EXTRACT(json_data, "$.startup") = "test company";
Fiddle

I want to prevent specific value insert into ulog2_ct table

I'm using ulog2 with mysql for gathering nf_conntrack values to DB in my bridge machine.
All things are works well, but I want to prevent to insert specific value which has "right(hex(orig_ip_saddr),8)='7f000001'".
I just execute query as "delete from ulog2_ct where right(hex(orig_ip_saddr),8)='7f000001' " periodically but its not good solution.
ulog2 provide queries to make and insert into the specific DB as https://github.com/inliniac/ulogd2/blob/master/doc/mysql-ulogd2-flat.sql
But I can't fully understand because I'm not sql expert.
I think some additional query would resolve what I want but I don't know how.
Insert query of mysql-ulogd2-flat.sql is as below: (line 435)
BEGIN
INSERT INTO ulog2_ct (oob_family, orig_ip_saddr, orig_ip_daddr, orig_ip_protocol,
orig_l4_sport, orig_l4_dport, orig_bytes, orig_packets,
reply_ip_saddr, reply_ip_daddr, reply_ip_protocol,
reply_l4_sport, reply_l4_dport, reply_bytes, reply_packets,
icmp_code, icmp_type, ct_mark,
flow_start_sec, flow_start_usec,
flow_end_sec, flow_end_usec)
VALUES (_oob_family, _orig_ip_saddr, _orig_ip_daddr, _orig_ip_protocol,
_orig_l4_sport, _orig_l4_dport, _orig_bytes, _orig_packets,
_reply_ip_saddr, _reply_ip_daddr, _reply_ip_protocol,
_reply_l4_sport, _reply_l4_dport, _reply_bytes, _reply_packets,
_icmp_code, _icmp_type, _ct_mark,
_flow_start_sec, _flow_start_usec,
_flow_end_sec, _flow_end_usec);
RETURN LAST_INSERT_ID();
END
Seo.
Just create a trigger on ulog2_ct
(check for syntax validation , as I have just typed it here)
CREATE TRIGGER trig_ulog2_ct
BEFORE INSERT ON ulog2_ct
FOR EACH ROW
BEGIN
DECLARE msg VARCHAR(255);
IF (right(hex(new.orig_ip_saddr),8)='7f000001') THEN
SIGNAL 'your error message Not to insert'
END IF;
END
Let us know

MySQL: Triggers leading to Error 1292 (truncated incorrect DOUBLE value) when inserting?

Can you help me resolve the error 'Truncated incorrect DOUBLE value' (Error Code 1292) in MySQL 6.3? I've searched for a solid hour and can't find a suitable answer in all the similar questions:
My task is to insert triggers into a database of movies. If someone tries to update an existing value or insert a new value into the schema 'made_money', then it is to be assured that the category falls into one of four base categories (see code). If it doesn't, default to "Action". I built and executed two triggers for this (because I don't know how to put UPDATE and INSERT into a single trigger):
delimiter //
CREATE TRIGGER movie_categories_A BEFORE UPDATE ON made_money
FOR EACH ROW
BEGIN
IF NEW.Category NOT IN ("Romantic", "Comedy", "Drama", "Action") THEN
SET NEW.Category = "Action";
END IF;
END;//
delimiter ;
delimiter //
CREATE TRIGGER movie_categories_B AFTER INSERT ON made_money
FOR EACH ROW
BEGIN
IF Category NOT IN ("Romantic", "Comedy", "Drama", "Action") THEN
SET Category = "Action";
END IF;
END;//
delimiter ;
Also, a new entry into 'log_data' has to be made when a new value is inserted into 'made_money', containing only the 'Movie' and 'Category' values.
delimiter //
CREATE TRIGGER condensed_test AFTER INSERT ON made_money
FOR EACH ROW
BEGIN
INSERT INTO log_data
SET Movie = NEW.Movie AND Category=NEW.Category;
END;//
delimiter ;
The three triggers all executed, although I'm not sure whether they are erroneous. Now my task is to insert a new entry into 'made_money':
INSERT INTO made_money (MOVIE,HOW_MUCH,DAY_OPENED,Category)
VALUES ('Iron Man', 1000000, '2008-05-02', 'ACTON');
I know that 'ACTON' is misspelled (I guess this is part of the exercise), yet unfortunately, this results in the following error:
Error Code: 1292. Truncated incorrect DOUBLE value: 'Iron Man'
The same happens if I spell 'Action' correctly. Please let me know if you have any ideas.
Best,
Jan
Try to change datatype for Movie, try by making it Text,
you can refer Similar issue
Please check the datatype 'made_money.Movie' and 'log_data.Movie'.
Please check the collation.

Matlab Database QueryDb error

I need to insert some data into a mysql database. Db is connected and working.
I am running the following code :
a = sprintf('%s',hashedStr);
sqlQueryStr = 'insert into products (security_code) values (a)'
QueryDB(sqlQueryStr);
I have a database called test and a table named products with 2 fields id and security_code.
When I run this, I get :
Unknown column 'a' in fieldlist ...
Why is this happening ? i dont have and dont need this column ...
Any help ?
Try with:
sqlQueryStr = sprintf('insert into products (security_code) values ("%s")',hashedStr);
QueryDB(sqlQueryStr);
problem is that you are not replacing "a" variable into sql expression

MySQL create "before insert" trigger failing and raising errors in triggers

I have successfully created an insert before trigger on a table using the innodb engine which "throws" an error by performing an insert on a non-existent table. The problem is that as I try to run the database create script on the production server it fails because the insert table does not exist. However, the same script runs fine on my workstation which makes me think that there is a MySQL config setting which is causing the create script to fail.
The question my issue brings up is whether the production server is compiling the trigger and my workstation is not (or compiling at runtime). On a production environment I would prefer to have the SQL compiled when created.
My method for simulating raising an error in a trigger is to DECLARE an integer variable and store a string in it. If you have SQL_MODE = STRICT_ALL_TABLES, this raises an error. Otherwise it raises a warning.
use test;
drop table if exists foo;
create table foo (id serial primary key, bar varchar(10));
drop trigger if exists RaiseError;
delimiter //
create trigger RaiseError before insert on foo
for each row
begin
declare bar int;
if new.bar = 'boom' then
set bar = 'You cannot store that value!';
end if;
end //
delimiter ;
set sql_mode = strict_all_tables;
insert into foo (bar) values ('boom');
Notice I named my local variable the same as the column bar that I want to test. That way the name of the variable appears in the error message, it's the same as the column:
ERROR 1366 (HY000): Incorrect integer value: 'You cannot store that value!' for column 'bar' at row 1
It turns out the length of the table name was causing the problem. The two server versions were from different releases with the server being a previous release (5.0 vs 5.1).
As for raising errors in triggers I have taken a different approach, however one that may be flawed depending on how MySQL handles the create trigger statement. If the trigger statements are verified on creation then this will fail.
Once an error is encountered (expected error) I set a session variable (variable name prefixed by an #) with the error message to display. Once the error message is set I do an insert into the non-existing table Die to raise an error in MySQL.
The application then catches the database error and performs a query on the error session variable. Based on the result I either throw an exception with the error message, the database error message or have the query quietly fail leaving the calling code to handle the failed query.
MySQL Trigger:
CREATE TRIGGER Error_Trigger
BEFORE INSERT ON Fubar
FOR EACH ROW
BEGIN
if DataIsBad then
set #Err = 'The data is fubared.';
insert into Die values (1);
end if;
END$$
Application side Code:
public function getDatabaseErrorMessage($AdminMessage = false){
if ($this->db->_error_number() != 0){
$AdminError = $this->db->_error_message();
$Query = $this->db->query("select #Err");
if ($Query){
$Error = $Query->row_array();
}
if (isset($Error["#Err"]) && $Error["#Err"] != ""){
$Error = $Error["#Err"];
$this->db->query("set #Err = ''");
} else {
if ($AdminMessage){
$Error = $AdminError;
} else {
return false;
}
}
throw new Exception($Error);
}
}