0

I am trying to create a public string that will be used in other parts of my app. I have tried to review the use of cursors but I couldn't find out how it would return what I wanted. I just need this to be a public method to return the value of the column email where the rowId = 1.

How far off-base am I with this and please give me some pointers? This method is inside my DatabaseHandler class

public String getUserName(String email) {

    SQLiteDatabase db = this.getReadableDatabase();
    String memail = 
            db.rawQuery("SELECT _email FROM login WHERE key_id = 1;", null);
    return memail;
}
Sam
  • 86,580
  • 20
  • 181
  • 179
KDEx
  • 3,505
  • 4
  • 31
  • 39
  • have a look "public Cursor rawQuery (String sql, String[] selectionArgs) Since: API Level 1 Runs the provided SQL and returns a Cursor over the result set. Parameters sql the SQL query. **The SQL string must not be ; terminated** " , [Reference](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html). – Imran Rana May 25 '12 at 19:31

2 Answers2

1

Use of cursor:

String q = "SELECT * FROM login where(key_id = 1)";
Cursor c = db.rawQuery(q, null); //  db is a object of SQLiteDatabase type  

Then retrieve value column email like:

              if(c.getCount() == 0)
              {                   
                 //No entry found
              }
              else  {
                  c.moveToFirst();
                  do {
                      String mail = c.getString(c.getColumnIndex("email")); 
                        } while (c.moveToNext());               
            c.close();

Use it in your method and return the value of String mail :)

Imran Rana
  • 11,899
  • 7
  • 45
  • 51
0

You need to use cursor as below:

public String getUserName(String email) {
    String email = null;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT _email FROM login WHERE key_id = 1;", null);
    if(c.moveToFirst())
        email = c.getString(0);
    c.close();

    return email;
}

But your method signature is a little weird. Your method says getUserName with input parameter saying email and returning email.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • You're right that is my mistake. I was just taking a shot in the dark here after many attempts. – KDEx May 25 '12 at 19:13
  • @Waqas have a look "public Cursor rawQuery (String sql, String[] selectionArgs) Since: API Level 1 Runs the provided SQL and returns a Cursor over the result set. Parameters sql the SQL query. **The SQL string must not be ; terminated** " , [Reference](http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html). BTW I'm still confuse why I was downvoted in an answer [here](http://stackoverflow.com/questions/10743660/database-help-needed/10744228#comment13967847_10744228) for using **rawQuery()** method instead of **query()** – Imran Rana May 25 '12 at 19:23
  • is it the reason you down-voted my answer? there's nothing wrong if @KDEx wants to use rawQuery instead of query. My goal was to tell him how to use Cursor. – waqaslam May 25 '12 at 19:27
  • No I was just pointing to **The SQL string must not be ; terminated** – Imran Rana May 25 '12 at 19:33
  • Is this how you point out things? I dont think semi-colon matters, or even if it does then you could simply write a comment for correction, because it is not what the question was about. The move you made shows how **NOOB** you are – waqaslam May 25 '12 at 19:44
  • I'm pretty new to SO and trying to learn(gathering experience of how to use so) from all of you. As I pointed out earlier I was downvoted for simply using **rawQuery()** (which is not what the question was about) [here](http://stackoverflow.com/questions/10743660/database-help-needed/10744228#comment13967847_10744228) from a well reputated user and users from chat rooms supported it. So, when I saw your answer, I thought I should downvote it from my past experince (maybe a bad experince IMHO). Don't take it personal please. I had to pass through conflicting situations, hope you understand. – Imran Rana May 25 '12 at 21:41