如何在Android SQLite中使用TRIGGER [英] How to use TRIGGER in Android SQLite

查看:173
本文介绍了如何在Android SQLite中使用TRIGGER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有两个表:

I have two tables in database:


  • 表一个名称和房号列

  • 表二有房号和时间栏。

现在删除或添加第一列中的房间号码时,我的第二个表格也应该更新。我认为这是可能的TRIGGER命令,但我不知道如何使用它。

Now when the room number from first column is deleted or added, my second table should also be updated. I think this is possible with TRIGGER command, but I am not really sure as how to use it.

一般来说,我的create database语句如下:

Generally my create database statement is like this:

private static final String DATABASE_CREATE_PATIENT_ID_TABLE =
    "create table " + DATABASE_PATIENT_TABLE +
    " (_id integer primary key autoincrement,"
     + "patient_number text not null, room_numbertext not null, " +
            "patient_initial text not null);";

现在,当房间被删除或添加到第一个表中时,我的第二个表应该更新。

Now when the rooms are deleted or added in the first table my second table should be updated.

private static final String DATABASE_CREATE_NOTES_ID_TABLE =
    "create table " + DATABASE_NOTES_TABLE +
    " (_id integer primary key autoincrement," +
     " room_number text not null, time_hour text not null, " +
            "notes_hour text not null, today_date text not null);";

最初我做的是比较两个表的内容。但这肯定会导致性能问题后,当数据将增加。所以我偶然发现了TRIGGER的东西。我想这可以解决我的问题,但我不知道该如何使用它。

Initially I was doing was compare the content of the two tables. But this definitely will lead to performance issue later when data will increase. So I stumbled across TRIGGER thing. I think this can solve my problem, but I don't know how exactly should I use it.

我从使用SQLite数据库与Android

我在我的另一个问题中解释了这个问题。请查看一下,如果请您指点一下。
新问题

推荐答案

简单的开始

create trigger simple_trigger1 after insert on database_patient_table begin update database_notes_table; end 
create trigger simple_trigger2 after delete on database_patient_table begin update database_notes_table; end

使用此文档 http://www.sqlite.org/lang_createtrigger.html

这篇关于如何在Android SQLite中使用TRIGGER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆