Java Android Studio-当sql查询搜索记录不存在时返回错误消息

Java Android Studio-当sql查询搜索记录不存在时返回错误消息,java,android,android-studio,mobile-application,Java,Android,Android Studio,Mobile Application,我创建了一个android应用程序,内置sqlite数据库,用户进行搜索并显示数据。一切都很好,但是当用户搜索的内容不存在时,我完全被困在弹出一条错误消息上 我不想什么都不返回,而是希望弹出一条消息说“记录不存在!” 这可能很简单,但我尝试的每个IF语句都有错误,如果有任何指针,我将不胜感激 我的查询代码: public String getProduct(String userInput){ c=db.rawQuery("select productname from Inven

我创建了一个android应用程序,内置sqlite数据库,用户进行搜索并显示数据。一切都很好,但是当用户搜索的内容不存在时,我完全被困在弹出一条错误消息上

我不想什么都不返回,而是希望弹出一条消息说“记录不存在!”

这可能很简单,但我尝试的每个IF语句都有错误,如果有任何指针,我将不胜感激

我的查询代码:

   public String getProduct(String userInput){
    c=db.rawQuery("select productname from Inventory where productname ='"+userInput+"'", new String[]{});
    StringBuffer buffer = new StringBuffer();
    while(c.moveToNext()){
        String product = c.getString(0);
        buffer.append(""+product);
    }
    return buffer.toString();
}

谢谢你的帮助

在执行while循环之前,您可以检查查询是否返回任何数据,如果不返回空字符串

如果(!c.moveToFirst())返回“”

使用
getProduct
时,可以检查它是否为空

String dbResult = getProduct(userInput);
if(dbResult.isEmpty) {
  // Display "Record does not exist!"
}

在while循环之前,您可以检查查询是否返回任何数据,如果不返回空字符串

如果(!c.moveToFirst())返回“”

使用
getProduct
时,可以检查它是否为空

String dbResult = getProduct(userInput);
if(dbResult.isEmpty) {
  // Display "Record does not exist!"
}