how to GET the SINGLE ROW with cursor in android?

无人久伴 提交于 2019-12-06 14:50:00

问题


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);


来源:https://stackoverflow.com/questions/10054987/how-to-get-the-single-row-with-cursor-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!