【问题标题】:Can't match string which stored in SQLite [duplicate]无法匹配存储在 SQLite 中的字符串 [重复]
【发布时间】:2016-07-03 09:48:08
【问题描述】:

在我的应用程序的一部分中,我从移动设备获取日期并将其作为字符串存储在 SQLite 数据库中。

当我尝试从数据库中选择具有相同日期的行时,找不到任何匹配结果。

同样在下面的代码中,我试图获取表格的特定单元格,我确定它与当前日期匹配。但没有匹配。

有什么想法吗?

从手机获取日期并将其作为字符串插入。

String currentDateString = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
MyDB.insertRow(currentDateString);

检查数据库中的日期是否等于移动日期。

if (MyDB.getRow(21).getString(1).toString()==currentDateString)
Toast.makeText(getBaseContext(),"Match",Toast.LENGTH_SHORT).show();

【问题讨论】:

    标签: android database android-sqlite


    【解决方案1】:

    在 Java 中使用 == 运算符无法匹配 Strings。您使用 .equals() 方法,因为 String 不是原始类型。

    所以试试这个 -

    if (MyDB.getRow(21).getString(1).toString().equals(currentDateString))
    

    【讨论】:

    • 谢谢,if语句没问题。
    • if 语句没问题。那么我如何更新数据库中的特定行(where 子句) public boolean updateRow(String Site_par,String AttDate_Par) { String where =AttDate + " = " + AttDate_Par;内容值 newValues = new ContentValues(); newValues.put(Site,Site_par); newValues.put(AttDate, AttDate_Par); // 将其插入数据库。返回 db.update(DATABASE_TABLE, newValues, where, null) != 0; }
    【解决方案2】:

    == 运算符不匹配字符串,因为它是一个对象。

    试试

     MyDB.getRow(21).getString(1).toString().equals(currentDateString)
    

    如果您需要它不区分大小写

     MyDB.getRow(21).getString(1).toString().equalsIgnoreCase(currentDateString) 
    

    还有 确保插入的日期格式与您匹配的日期相同

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多