How to use TRIGGER in Android SQLite

前端 未结 3 1280
夕颜
夕颜 2020-12-10 06:04

I have two tables in database:

  • table one has name and room number column
  • table two has room number and time column.

Now when the r

3条回答
  •  醉梦人生
    2020-12-10 06:22

    Demo for Sqlite Trigger in Android HERE

    Trigger are some procedural code executed after certain event occur in our database.

    I have wrote a sample demo for trigger.

    Example: Consider a database of any University. So if any Student record is added in student table , new row(tuple) is added automatically in library section or canteen section etc.

    So by writing a simple trigger we can automatically insert new records in other sections avoiding boiler plate code.

    Schema

     CREATE TABLE student (sid INTEGER PRIMARY KEY, sname TEXT)  
     CREATE TABLE canteen (cid , sid )  
     CREATE TABLE library (lid INTEGER PRIMARY KEY, sid TEXT)
    

    Trigger to automatically add records in library and canteen table:

    CREATE TRIGGER if not exists add_student   
       AFTER INSERT  
     ON[student]  
       for each row  
         BEGIN  
            insert into library values (2 , new.sid );  
            insert into canteen values (3 , new.sid);  
         END; 
    

    Explanation:The concept here is to create a trigger ,which insert the values in canteen and library based on new student id.

提交回复
热议问题