Android Sqlite:检查表中是否存在行 [英] Android Sqlite: Check if row exists in table

查看:126
本文介绍了Android Sqlite:检查表中是否存在行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组,如果在插入它们之前存在于表中则需要进行检查,以避免重复。什么是SQL查询以及如何将以下值替换为它? :)

I have an array of strings that need to be checked if exists in a table before inserting them in order to avoid duplicates. What is the SQL query and how do I substitute the following values to it? :)

ArrayList<Product> NewProducts= new ArrayList<Product>();

我的产品型号:

public class Product {
    public Product()
    {

    }

    public String PID = "pid";
    public String getPID() {
        return PID;
    }
    public void setPID(String pID) {
        PID = pID;
    }
    public String getNAME() {
        return NAME;
    }
    public void setNAME(String nAME) {
        NAME = nAME;
    }

    public String PID = "pid";
    public String NAME = "name";
}

表名: product_pics

Table name: product_pics

数据库名称: product_db

Database name: product_db

我知道此声明可行:

"SELECT * FROM ' + product_pics + ' WHERE PID=' + pid +'"

如何正确格式化,以便在产品存在时方法返回?

How do I properly format this such that the method returns if the product exists or not?

推荐答案

就像

 Cursor cursor = null;
 String sql ="SELECT PID FROM "+TableName+" WHERE PID="+pidValue; 
 cursor= db.rawQuery(sql,null);
 Log("Cursor Count : " + cursor.getCount());

 if(cursor.getCount()>0){
  //PID Found
 }else{
 //PID Not Found 
 }
 cursor.close();

这篇关于Android Sqlite:检查表中是否存在行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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