如何在Android SQLite中使用TRIGGER
我在数据库中有两个表:
- 表一有名称和房间号列,
- 表二有房间号和时间列。
现在,当删除或添加第一列中的房间号时,我的第二个表也应该更新。我认为这可以通过 TRIGGER 命令实现,但我不太确定如何使用它。
一般来说,我的创建数据库语句是这样的:
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);";
现在,当在第一个表中删除或添加房间时,我的第二个表应该更新。
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 的事情。我认为这可以解决我的问题,但我不知道我到底应该如何使用它。
我已经在另一个问题中用屏幕截图解释了这个问题。请看一下,如果请指导我 新问题
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 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.
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);";
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.
I came to know about it from Using SQLite Database with Android.
I have explained this problem with the screen shot in my another question. Please have a look at it and if please kindly guide me
new question
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单开始使用
此文档 http://www.sqlite.org/lang_createtrigger.html
Simple start for you
Use this documentation http://www.sqlite.org/lang_createtrigger.html
根据您的应用运行的 SQLite 版本,您也许可以使用SQLite 的外键支持。
在旧版本的 SQLite 中,您可能可以使用 genfkey< /a> 实用程序来创建触发器来强制外键约束(旧版本的 SQLite 会解析在 CREATE TABLE 语句期间添加的外键约束,但不会实际实现它们)。
Depending on which version of SQLite your app is running on, you might be able to use SQLite's foreign key support.
In older version's of SQLite you might be able to use the genfkey utility to create triggers to enforce your foreign key constraints (older versions of SQLite would parse foreign key constraints added during a
CREATE TABLE
statement, but wouldn't actually implement them).触发器是一些在我们的数据库中发生特定事件后执行的程序代码。
我已经为触发器编写了一个示例演示。
示例:考虑任何大学的数据库。因此,如果在学生表中添加任何学生记录,则会在图书馆部分或食堂部分等中自动添加新行(元组)。
因此,通过编写一个简单的触发器,我们可以自动在其他部分中插入新记录,从而避免样板代码。
架构
自动在图书馆和食堂表中添加记录的
触发器:说明:这里的概念是创建一个触发器,根据新的学生 ID 插入食堂和图书馆中的值。
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
Trigger to automatically add records in library and canteen table:
Explanation:The concept here is to create a trigger ,which insert the values in canteen and library based on new student id.