如何在Sqlite数据库中搜索值?

时间:2018-08-31 19:27:23

标签: java android android-sqlite

我正在开发一个用于联系人的应用程序,为此,我使用sqlite数据库存储姓名和电话号码,因此我想过滤姓名和电话号码以搜索 我尝试了该查询,但 这对我不起作用 我使用过像“ =”和“ =”一样的问题的任何解决方案吗?

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

公共类FeedReaderDbHelper扩展了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);


}

}

1 个答案:

答案 0 :(得分:0)

目前尚不清楚您想要的是什么,但是如果您只想获取具有给定值的行,它应该像这样:

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;

它可以在我的应用中使用