从sqlite触发器调用Java方法(android) [英] Calling a Java method from a sqlite trigger (android)

查看:300
本文介绍了从sqlite触发器调用Java方法(android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想使用sqlite触发器
注册回调方法,

I want registered callback method using sqlite trigger for example,

public void printLog(){
 Log.i("TAG","1 row added");
}

此方法在sqlite中插入任何行后调用。

this method calling after insert any row in sqlite.

有可能吗?

该怎么做?

推荐答案

SQLite提供了数据更改通知回调。我认为Android不会直接公开它们,但确实有例如CursorAdapter提供一些更改通知。

SQLite provides Data Change Notification Callbacks. I don't think that Android exposes them directly but it does have for example CursorAdapter which provides some change notifications.

您还可以使用 getContentResolver()。registerContentObserver ,但不幸的是,它不会告诉您进行了哪种更改,它可能是删除,插入或更新。

You can use also use the getContentResolver().registerContentObserver but unfortunately it doesn't tell you what kind of change was made, it could be a delete, insert or update.

如果您控制ContentProvider,与DB进行接口,则可以触发一个Intent或使用 getContentResolver()。notifyChange 发送特殊的Uri通知,该通知同时标识表和操作。您可以通知的Uri示例可能是:content:// my-authority / change / table-name / insert

If you control the ContentProvider that interfaces with the DB then you could fire an Intent or use getContentResolver().notifyChange to send a special Uri notification that identifies both the table and action. An example Uri you could notify with might be: content://my-authority/change/table-name/insert

但是即使这样,您也不知道到底是哪个

But even then you don't know exactly which rows were effected by the change.

似乎触发器(写入更改日志表)似乎可以确保您知道所有更改,无论它们来自何处,而且您可以知道发生的确切ID和操作。不幸的是,这意味着插入/更新/删除速度较慢,这意味着您可能需要某种服务来处理和删除更改。

Seems like triggers that write to a change log table will guarantee you hear about all changes regardless of where they came from, and you can know the exact id and action that occurred. Unfortunately it means slower inserts/updates/deletes and it means you probably need a Service of some kind to process and delete changes.

我很想听听这些是否一些更好的解决方案!

I'd love to hear if these is some better solution out there!

这篇关于从sqlite触发器调用Java方法(android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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