【问题标题】:SQLite Database Search Not Returning RowsSQLite 数据库搜索不返回行
【发布时间】:2012-10-12 15:40:20
【问题描述】:

我有一个方法,我想将一个字符串传递给它并在数据库中搜索匹配项。我是数据库新手,所以我束手无策。几天来,我一直在谷歌搜索并尝试不同的查询,但它只是不起作用。游标创建得很好,所以我不认为查询有任何语法问题。光标不为空,那里有一个 Cursor 对象,那里也有所有 3 列。但是没有行。当我传入一个我知道应该匹配的字符串时,它只是不返回任何数据。方法如下:

public Business getBusiness(String search) {
    File database=currentContext.getDatabasePath(DATABASE_NAME);
    SQLiteDatabase db = SQLiteDatabase.openDatabase(database.getAbsolutePath(),     
                                                        null, Context.MODE_PRIVATE);

    Cursor cursor = db.query(TABLE_NAME, new String[] {COLUMN_ONE, COLUMN_TWO,        
                             COLUMN_THREE}, COLUMN_ONE + " like ' " + search + " '", 
                             null, null, null, null);

    if (cursor != null)
        cursor.moveToFirst();

    Business business = new Business(currentContext, cursor.getString(0),  
                                     cursor.getString(1), cursor.getString(2),  
                                     cursor.getInt(3), cursor.getInt(4));
    db.close();

    return null;
}

【问题讨论】:

    标签: android android-sqlite


    【解决方案1】:

    尝试更改您的like 语句以包含通配符

    例如改变

    " like ' " + search + " '"
    

    " like ' " + search + "_'"
    

    " like '_" + search + "_'"
    

    LIKE 模式中的百分号(“%”)匹配字符串中任何零个或多个字符的序列。 LIKE 模式中的下划线(“_”)匹配字符串中的任何单个字符。

    相信这是有帮助的。祝你好运

    【讨论】:

      【解决方案2】:

      你已经尝试过这段代码......这段代码运行正常..

      Cursor cursor;
      
                  try
                  {
                      cursor = db.query
                      (
                              TABLE_NAME,
                              new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO, TABLE_ROW_THREE, TABLE_ROW_FOUR, TABLE_ROW_FIVE, TABLE_ROW_SIX },
                              TABLE_ROW_ID + "=" + rowID,
                              null, null, null, null, null
                      );
      
                      cursor.moveToFirst();
      
                      if (!cursor.isAfterLast())
                      {
                          do
                          {                   
                              String row_item_id=cursor.getLong(0);
                              String row_item_name=cursor.getString(1);
                              String row_item_basic_price=cursor.getFloat(2);
                              String row_item_tax=cursor.getFloat(3);
                              String row_item_shipping=cursor.getFloat(4);
                              String row_item_price=cursor.getFloat(5);
                              byte[] imgByte = cursor.getBlob(6);
                                  Bitmap row_item_bitmap=BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
      
                          }
      
                          while (cursor.moveToNext());
                      }
                      cursor.close();
                  }
                  catch (SQLException e) 
                  {
                      Log.e("DB ERROR", e.toString());
                      e.printStackTrace();
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-03
        • 1970-01-01
        • 1970-01-01
        • 2015-05-16
        • 1970-01-01
        • 2011-12-25
        相关资源
        最近更新 更多