sqlite3 got syntax error while executing vacuum in a trigger - sqlite

I'm using sqlte3.8.8, trying to create a trigger to clean old data. Here is the SQL that I put in:
CREATE TRIGGER "main"."NewTrigger" AFTER INSERT ON "historydata"
BEGIN
delete from historydata where id in (select id from historydata order by id limit 100000);
vacuum;
END;
But I got Syntax error on "vacuum;".However, it works fine in sqlite command line.
Is it the case that "vacuum" cannot be used in a trigger?

The documentation shows that only UPDATE/INSERT/DELETE/SELECT statements are allowed in a trigger body.

Related

Trigger compiled successfully but still not firing as desired

Here is my simple PL/SQL code for demonstration purpose.
create table cust(cname varchar(10));
set SERVEROUTPUT ON;
create or replace trigger tgr
before insert on cust
for each row
enable
begin
dbms_output.put_line('Trigger hit on insert');
end;
/
insert into cust values('John');
OUTPUT:/
Table CUST created
Trigger TGR compiled
1 row inserted // *EXPECTING* Trigger hit on insert
You can't use DBMS_OUTPUT.PUT_LINE because when you insert data to your table, there is no prompt or screen which shows the execution of trigger. this applied same for procedures , functions , triggers.
Don't use commit inside a table. it will throw an exception. if you want to use commit then make the trigger pragma autonomous_transaction (i suggest not to use commit.it will automatically commit) .
Also make sure you don't modify the same table in the trigger. it will also throw and exception for mutating the data.
sample code
create table cust(cname varchar(10));
create table log(log varchar(10));
create or replace trigger tgr
before insert on cust
for each row
enable
begin
insert into log values('test');
end;
/
Don't use DBMS_OUTPUT.PUT_LINE inside a trigger to check if it was fired or not. Some of the IDEs like SQL developer might not output the message , even if you use SET SERVEROUTPUT ON.
However it could work on sqlplus . When i tried, it did display the message in sqlplus. So, it is not an issue with the database or your trigger.
SQL> insert into cust values('John');
Trigger hit on insert
And if you issue commit or rollback on the transaction, the messages might appear in sql developer.
I would suggest , it is better if you can create a log table and try to insert records into it rather than use dbms_output.
begin
INSERT INTO LOG_TABLE ( log_date,log_message) VALUES (SYSDATE,'Trigger hit on insert');
end;
/

plsql Trigger error ORA-00604

Following Trigger created in a user anu
create or replace trigger audit_creation1
before create
on schema
begin
insert into audit_creation
values(audit_creation_s1.nextval,
ora_dict_obj_owner,
ora_dict_obj_name,
sysdate);
end;
create table cc(cid number);
ORA-00604: error occurred at recursive SQL level 1
ORA-30511: invalid DDL operation in system triggers
This worked before many times. It was working successfully.
But now it's throwing error.
Throw a command
purge recyclebin;
as your trigger owner (anu in your case).

Create triggers with RAISE

I'm trying to limit a table to only one record and disable all attempt to add more.
I have created this trigger: CREATE TRIGGER abort_insert_to_my_tbl BEFORE INSERT ON my_tbl
BEGIN
RAISE(ABORT,"You can't add records to my_tbl")
END;
But I keep getting this error:
Error: near line 3080: near "RAISE": syntax error
What am I doing wrong?
As the documentation shows, RAISE is a function, not a statement, so it cannot be used directly in the trigger body.
To use a function in a statement, use, for example, a SELECT statement:
CREATE TRIGGER abort_insert_to_my_tbl
BEFORE INSERT ON my_tbl
BEGIN
SELECT RAISE(ABORT, 'You can''t add records to my_tbl');
END;

Obtaining inserted sqlite from command line

I use bash to access a sqlite3 database.
Is there a way to get the last inserted id that way, because on the sqlite-webpage (http://www.sqlite.org/c3ref/last_insert_rowid.html) I only see the C-API, but no way to do it on the command line.
The function last_insert_rowid() does not work either:
sqlite> last_insert_rowid();
SQL error: near "last_insert_rowid": syntax error
should be
select last_insert_rowid();

No table found error in SQlite

I have a problem with SQlite. Whenever I execute Queries it goes fine but when I exit by .exit command and open again and execute Select * form emp statement, it renders no table even though I use begin transaction and commit .
Any help please. I'm new to SQLite.
Thanks
Bamadeva
I got the solution:-
Wherever our database is created, in my case it is :-
C:\Sites\databases>
Since my database is contactlist_development, I should have entered it with the following commands:-
C:\Sites\databases>sqlite3 contactlist_development
This takes me to :-
sqlite>.schema
Then i am able to view the table details which i just created:)

Resources