Select query with condition

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Select query with condition

Postby dennie » Fri Aug 07, 2009 6:16 am

Hi guys!
I'm try to create an simple application use database, I use the SELECT query but it result is no data on my ListView. I don't know, maybe I forgot something?
This is my code:

Code: Select all
package com.original.testdata;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class TestDbHelper extends Activity {
   
   public static final String KEY_ID = "_id";
   public static final String KEY_TITLE = "title";
   public static final String KEY_TITLE_RAW = "title_raw";   
   
   private static final String DATABASE_NAME = "data.db";   
   private static final int DATABASE_VERSION = 2;
   String[] FROM = {KEY_ID, KEY_TITLE, KEY_TITLE_RAW};
    private static final String TABLE_NAME = "search";
    private SearchData mDbHelper;
    private Context mCtx;
    private SQLiteDatabase mDb;
   
   private static class SearchData extends SQLiteOpenHelper{             
       
      public SearchData(Context context) {
         super(context, DATABASE_NAME, null, DATABASE_VERSION);         
      }      

      @Override
      public void onCreate(SQLiteDatabase db) {
         db.execSQL("create table search(_id integer primary key not null," +
               " title varchar(50) not null," +
               " title_raw varchar(100) not null" +
               ");");
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
         db.execSQL("CROP TABLE IF EXISTS search");
         onCreate(db);
      }       
    }
   public TestDbHelper open() throws SQLException {
        mDbHelper = new SearchData(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
   
    public void close() {
        mDbHelper.close();
    }
    public TestDbHelper(Context ctx) {
        this.mCtx = ctx;
    }

   public Cursor getData(){    
       SQLiteDatabase db = mDbHelper.getReadableDatabase();
       String select = "Select _id, title, title_raw from search Where(title_raw like " + "'%Smith%'" +
      ")";       
       Cursor cursor = db.query(TABLE_NAME, FROM,
             select, null, null, null, null);
       startManagingCursor(cursor);
       return cursor;
    }   
   
}
dennie
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Jul 18, 2009 8:10 am

Top

Postby nmc » Fri Aug 07, 2009 9:42 am

When specifying a full sql statement, get the Curor object from rawQuery() instead of query()
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby dennie » Fri Aug 07, 2009 1:04 pm

I found that my query are not true, it must like this
Code: Select all
Cursor cursor = db.query(TABLE_NAME, new String[] {"_id", "title", "title_raw"},
                "title_raw like " + "'%Smith%'", null, null, null, null);


Anyways, thanks for your advice!
dennie
Junior Developer
Junior Developer
 
Posts: 12
Joined: Sat Jul 18, 2009 8:10 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 2 guests