android - 检查数据库中是否存在值

标签 android

我想检查数据库中是否存在一些值。如果是这样,如果游标为空,则该方法应返回 TRUE 或 FALSE。但问题是,尽管值不在数据库中,但它始终返回 TRUE!我错过了什么?

    // This method check if the combination image path and contact name already exists in database
public boolean checkContentDatabase(String imageFilePath, String contactName) {

    String query = "Select * from " + DB_TABLE+ " where " + TABLE_IMAGE_PATH + "='" + imageFilePath + "' and " + TABLE_CONTACT_NAME + "='" + contactName +"' ;";

    Cursor c = db.rawQuery(query, null);

    if(c != null) // Exists in database
     {
     return true;
     }
     else
     {
         return false;
     }
}

最佳答案

将您的 if 条件替换为:

if(c.getCount() > 0)
 {
 return true;
 }
 else
 {
     return false;
 }

关于android - 检查数据库中是否存在值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15218867/

相关文章:

android - 向布局添加颜色背景和边框半径

java - Android 秒表代码更有效的方法?

android - 如何在显示之前完成一个 Activity ?

java - 处理程序 - postDelayed 执行两次

java - ArrayList set(position,object) 方法 RecyclerView 中的 IndexOutOfBound 异常

java - 从 MongoDB 删除时出现 com.mongodb.MongoTimeoutException

android - 通过 Intent 将信息传递给 Google Map

android - Android应用程序开发中如何使用加速度计测量距离

android - 将 MySQL 查询转换为 SQLite

安卓应用自卸载