机器人SQLite的查询检测当元件不存在于表中存在 [英] Android Sqlite query detect when an element doesn't exists in the table

查看:111
本文介绍了机器人SQLite的查询检测当元件不存在于表中存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检测当一个元素没有在我的表存在吗?我需要的,因为我想更新就可以了/插入联系人。我的问题是,我想插入使用ContentObserver一个新的接触,但这种元素被称为多次,我选择的最后一个元素。所以,当我插入一个新的联系人,我选择了最后一个元素,我试图确定是否在数据库中存在并将其插入。

How can I detect when an element doesn't exist in my table? I need because I want to update/insert contacts on it. My problem it's that I want to insert a new contact by using ContentObserver but this element is called multiple times and I'm selecting the last element. So when i insert a new contact, I select the last element, I'm trying to identify if exists on the db and insert it.

推荐答案

使用一个布尔值来检查该联系人是否存在或不

use a boolean value to check whether the contact exist or not

boolean contact = myDbHelper.checkidExitsorNot(ur table name,row name , value);

public boolean checkidExitsorNot(String tablename, String rowname, String id) {
    String queryf = "select * from " + tablename + " where " + rowname  + "='" + Integer.valueOf(id) + "'";

    Cursor c = myDataBase.rawQuery(queryf, null);
    if (c.getCount() == 0) {
        c.close();
        return true;
    }else {
        c.close();
        return false;
    }
}

如果返回的是真的,那么它不假,如果存在,它存在

if the return is true then it does not exist if false it exist

这篇关于机器人SQLite的查询检测当元件不存在于表中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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