简体   繁体   中英

How to search value in Sqlite database?

I'm developing an app which is used for contacts and for this i used sqlite database to store name and number so i want to filter name and number for searching for which i tried that query but it's not working for me any solution for problem i used like and "=" both?

public boolean onQueryTextChange(String newText) {

  try {
       final String temp = newText.toLowerCase();


      final ArrayList<Map> map1 = new ArrayList();


      try {
          dbhelper =new FeedReaderDbHelper(getActivity());

          SQLiteDatabase db=dbhelper.getReadableDatabase();



          String projection[]={FeedReaderContract.FeedEntry.COLUMNS_TITLE,FeedReaderContract.FeedEntry.COLUMN_SUB_TITLE};
          Cursor cursor =db.query(FeedReaderContract.FeedEntry.TABLE_NAME,projection ,FeedReaderContract.FeedEntry.COLUMNS_TITLE+"= ?",new String[]{newText},null,null,null);

          while(cursor.moveToFirst()){


            String  name = cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMNS_TITLE));
              String number = cursor.getString(cursor.getColumnIndex(FeedReaderContract.FeedEntry.COLUMN_SUB_TITLE));



              Map<String, Object> map = new HashMap();
              map.put(name, number);
              if (name.toLowerCase().contains(temp)) {
                  map1.add(map);
                  Phone.this.adapter.Filter(map1);

              }





          }    }
      catch(Exception e){
              System.out.print(Retriving_list);

          }












  }catch(Exception e){


      Toast.makeText(getActivity(), First_Retrive_Data, Toast.LENGTH_SHORT).show();



  }

//DbHelperClass

public class FeedReaderDbHelper extends SQLiteOpenHelper{

public static final int DB_VERSION =1;

public static String getDataBASE_NAME() {
    return DataBASE_NAME;
}

public static void setDataBASE_NAME(String dataBASE_NAME) {
    DataBASE_NAME = dataBASE_NAME;
}

public static  String DataBASE_NAME;


Context context;





private static final String SQL_CREATE_ENTRIES =
        "CREATE TABLE " + FeedReaderContract.FeedEntry.TABLE_NAME + "(" +
                FeedReaderContract.FeedEntry._ID + "INTEGER PRIMARY KEY," +
                FeedReaderContract.FeedEntry.COLUMNS_TITLE + " TEXT ," +
                FeedReaderContract.FeedEntry.COLUMN_SUB_TITLE + " TEXT UNIQUE)";

private static final String SQL_DELETE_ENTRIES =
        "DROP TABLE IF EXISTS " + FeedReaderContract.FeedEntry.TABLE_NAME;






public FeedReaderDbHelper(Context context) {
    super(context, FeedReaderDbHelper.getDataBASE_NAME(), null, DB_VERSION);
    this.context=context;






}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

    sqLiteDatabase.execSQL(SQL_CREATE_ENTRIES);


}

void  addContactValues(FeedReaderDbHelper  dbhelper ,String name ,String number){
    SQLiteDatabase db=                   dbhelper.getWritableDatabase();

  ContentValues  values =new ContentValues();


    values.put(FeedReaderContract.FeedEntry.COLUMNS_TITLE ,name);
    values.put(FeedReaderContract.FeedEntry.COLUMN_SUB_TITLE,number);



 long rows=   db.insertWithOnConflict(FeedReaderContract.FeedEntry.TABLE_NAME,null,values,SQLiteDatabase.CONFLICT_IGNORE);

    if (rows==-1){


     //   Toast.makeText(context, String.valueOf(rows)+"not interested", Toast.LENGTH_SHORT).show();

    }

   else{

       Toast.makeText(context, String.valueOf(rows)+"inserted", Toast.LENGTH_SHORT).show();


   }







}



@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    sqLiteDatabase.execSQL(SQL_DELETE_ENTRIES);

    onCreate(sqLiteDatabase);


}

}

It's not clear what exact do you want, but if you want do get only rows with given values, it should looks like this:

String mSearchString = "what do you want to search";
String selection = setupSelectionString();
cursor = database.query(NoteContract.NoteEntry.NOTES_TABLE_NAME, projection, selection, selectionArgs,
                        null, null, sortOrder);
    private String setupSelectionString() {
        String selection = null;
        if (mSearchString != null) {
            selection = FeedReaderContract.FeedEntry.COLUMNS_TITLE + " LIKE '%"
                    + mSearchString + "%'"
                    + " OR " + FeedReaderContract.FeedEntry.COLUMN_SUB_TITLE + " LIKE '%"
                    + mSearchString + "%'";
        }
        return selection;

It works in my app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM