Tuesday, 12 March 2013

Get a single record from SQLite in ANDROID

For COMPLETE TUTORIALS see here :
in DATABASE HELPER class:
  String[] coloumns = new String[] { KEY_ROWID, KEY_NAME, KEY_NICKNAME };
      // calling elements    in an array
  Cursor c = db.query(DATABASE_TABLE, coloumns, KEY_ROWID + "=" + l,null, null, null, null); 
    if (c != null)
      {
         c.moveToFirst();
         String name = c.getString(1);
         // since name is in position 1 ie second coloumn
         return name;
      }
        return null;
in sqlite.java :
  String s =editinfo.getText().toString();
    long l=Long.parseLong(s);
    DBAdapter adapter=new DBAdapter(SQliteExample.this);
    adapter.open();
    String returnedname=adapter.returnName(l);
    String returnednickname=adapter.returnickname(l);
    adapter.close();
    editName.setText(returnedname);
    editNickName.setText(returnednickname);

No comments:

Post a Comment