how to GET the SINGLE ROW with cursor in android?

2019-04-16 05:44发布

问题:

i want to get the row from databas which name = jiten but is gives error in syntax can anyone tell me what is the right syntax for getting the single row here is my code for database class

public void getdata()
{


     Cursor cursor = myDataBase.query("emp", new String[] {"email","name"}, " name='jiten'",new String[]{}, null, null, null);

     Log.e("running", "cursor run");



     if(cursor!=null)
     {

         Log.e("running", "curosr is not null");
     while(cursor.moveToFirst())
     {

         Log.e("running", "curosr while loop enter");

         String temp =  (cursor.getString(cursor.getColumnIndex(email)));
             String temp2 =(cursor.getString(cursor.getColumnIndex(name)));
        Log.e("running", "id  email" +temp+ " name"+temp2);

    }
 }
}

回答1:

You are missing the selectionArgs

   public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

in your case it would be:

 Cursor cursor = myDataBase.query("emp", new String[] {"email","name"}, "name=?",new String[]{"jiten"}, null, null, null);