Pavan Anadkat
Pavan Anadkat

Reputation: 111

sqlite search query for finding a particular data from column?

I am making app for finding number plates of india. my database contains two columnns "code" and "city" code contains data like MH1,MH2 etc. and city contains data like Pune,Mumbai.

App contains one edittext box and listview.

Listview consists whole data from database like GJ3 Rajkot, GJ10 Jamnagar etc.

if i write GJ in edittext box whole only data of GJ must be apperaed in listview.

Upvotes: 3

Views: 4733

Answers (2)

Rahul Patel
Rahul Patel

Reputation: 3843

Try this :

Cursor c = mDb.rawQuery(
            "select * from table_name where column_name = ?",
            new String[] { "search" });

EDIT :

Follow the tutorial of the given above link and add below method into TestAdapter

public Cursor get_tag_Data()
 {
     try
     {
         String sql ="select * from table_name where column_name = ?", new String[] { edittext.getText().toString().trim()}";

         Cursor mCur = mDb.rawQuery(sql, null);
         if (mCur!=null)
         {
            mCur.moveToNext();
         }
         return mCur;
     }
     catch (SQLException mSQLException) 
     {
         Log.e(TAG, "getTestData >>"+ mSQLException.toString());
         throw mSQLException;
     }
 }

and call this method from your class like:

 TestAdapter mDbHelper = new TestAdapter(urContext);        
 mDbHelper.createDatabase();      
mDbHelper.open();

Cursor testdata = mDbHelper.get_tag_Data();

mDbHelper.close();

EDIT:

Declare below method into your database class,

 public List<String> getQuestions(String difficulty) {

   public static List<String> question_Set;
    question_Set = new ArrayList<String>();


    Cursor c = mDb.rawQuery(
            "select * from table_name where column_name = ?", new String[] { difficulty });

    while (c.moveToNext()) {

        question_Set.add(c.getString(1).trim());


    }

    return question_Set;
}

Now call like

DB.getQuestions(edittext.getText().toString().trim()); // DB is your database class name

Upvotes: 2

Jaiprakash Soni
Jaiprakash Soni

Reputation: 4112

Here is the query : SELECT * FROM table_name WHERE code LIKE '%GJ%' LIMIT 0 , 30

Upvotes: 2

Related Questions