Android SQLite SELECT Query

后端 未结 6 1289
我寻月下人不归
我寻月下人不归 2020-12-30 00:03

I have taken String value from a EditText and set it inside SELECT QUERY after WHERE condition

As

TextView tv          


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 00:29

     public User searchUser(String name) {
        User u = new User();
        SQLiteDatabase db = this.getWritableDatabase(); //get the database that was created in this instance
        Cursor c = db.rawQuery("select * from " + TABLE_NAME_User+" where username =?", new String[]{name});
        if (c.moveToLast()) {
            u.setUsername(c.getString(1));
            u.setEmail(c.getString(1));
            u.setImgUrl(c.getString(2));
            u.setScoreEng(c.getString(3));
            u.setScoreFr(c.getString(4));
            u.setScoreSpan(c.getString(5));
            u.setScoreGer(c.getString(6));
            u.setLevelFr(c.getString(7));
            u.setLevelEng(c.getString(8));
            u.setLevelSpan(c.getString(9));
            u.setLevelGer(c.getString(10));
            return u;
    
        }else {
            Log.e("error not found", "user can't be found or database empty");
            return u;
        }
    
    }
    

    this is my code to select one user and one only so you initiate an empty object of your class then you call your writable Database use a cursor in case there many and you need one here you have a choice Use : 1-

       if (c.moveToLast()) { } //it return the last element in that cursor 
    

    or Use : 2-

     if(c.moveToFirst()) {  } //return the first object in the cursor 
    

    and don't forget in case the database is empty you'll have to deal with that in my case i just return an empty object

    Good Luck

提交回复
热议问题