如何在android中使用光标获取SINGLE ROW?

时间:2012-04-07 13:43:23

标签: android sqlite cursor

我想从databas获取name = jiten的行 但是语法错误可以让任何人告诉我什么是正确的语法 获得单行 这是我的数据库类代码

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 个答案:

答案 0 :(得分:3)

您缺少selectionArgs

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

您可以在选择中包含?,它将被值替换   来自selectionArgs,以便它们出现在选择中。该   值将绑定为字符串。

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

在你的情况下,它将是:

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