2012-08-07 112 views
0

我有一个方法,我想传递一个字符串到它并搜索数据库的匹配。我是数据库的新手,所以我在我的智慧结束。我一直在使用googling并尝试不同的查询几天,现在它不工作。游标创建得很好,所以我不认为查询有任何语法问题。游标不为空,那里有一个Cursor对象,并且里面也有3列。但是没有行。当我传入一个字符串时,我知道应该有一个匹配它只是不返回任何数据。下面是方法:SQLite数据库搜索不返回行

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; 
} 

回答

1

访问This Link它包含了SQLite数据库

+0

感谢,我会检查出来。我已经阅读了一些教程,并且对getReadableDatabase()和getWritableDatabase()有个疑问。我试图使用这些方法来连接数据库,但是我遇到了问题,因为两者似乎都在调用onCreate(),它会调用数据库创建代码,然后抛出一个异常,因为数据库已经存在。这些方法是否应该调用onCreate()? – 2012-08-07 13:44:25

+0

将getWritableDatabase()方法调用到您的DbHelper类的构造函数中。 – Yash 2012-08-08 04:17:30

0

试着改变你喜欢的语句从

" like ' " + search + " '" 
包含通配符(S)

例如改变整个教程

" like ' " + search + "_'" 

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

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

相信这是协助。祝你好运

0

你试试这个代码...此代码运行正常..

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(); 
      }