简体   繁体   English

Android-检查数据库中是否存在一行

[英]Android - Check if a row within the DB exists

I've created a method where the app checks if a number exists in the DB: 我创建了一个方法,其中应用程序检查数据库中是否存在数字:

    Cursor player = playerDatabase.rawQuery("select * from playerTable where playerNumber ="+number,null);
  try {
      if (player.getCount() >= 0) {
          player.close();
          return true;
      }
      else{
          return false;
      }
  }finally {
      if(player != null){
          player.close();
      }
  }

But the problem is, this always returns true? 但是问题是,这总是返回true吗? The reason I am using it this way, is from the previous answers on Stack Overflow. 我之所以使用这种方式,是因为之前有关堆栈溢出的答案。 What is the optimal way of being able to check if a search query returns a row/check if this number exits? 能够检查搜索查询是否返回行/检查该数字是否存在的最佳方式是什么?

This is the most efficient way to find if a row exists or not. 这是查找行是否存在的最有效方法。

SELECT EXISTS(SELECT 1 FROM playerTable WHERE playerNumber="Here comes the player number");

It will return 1 if there is a row with playerNumber in the table or will return 0 if there is no row in the table. 如果表中有行有playerNumber的行,则返回1;如果表中没有行,则返回0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM