Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to search through a DB using a cursor( sqlite query) in android
    text
    copied!<p>I am an android beginner. I am trying to design this simple application which takes the name of a city from the user using an editText View , compares that in the database , and returns the ZIP code of that city. Now ,I'm having a problem with the implementation of cursors. Please help.How can i query the database to fetch the corresponding code.</p> <pre><code> EditText city; Button add,show1; RadioGroup choose; String k; @Override public void onCreate(Bundle savedInstanceState) { try { super.onCreate(savedInstanceState); setContentView(R.layout.main); //final ContentValues values = new ContentValues(); city=(EditText)findViewById(R.id.editText1); add=(Button)findViewById(R.id.add); choose=(RadioGroup)findViewById(R.id.radio01); show1=(Button)findViewById(R.id.button1); k=city.getText().toString(); createDatabase(); } catch(Exception e) { } show1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub db=openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null); try{ if(k == "CITY") { String[] result_columns=new String[]{"_id","CITY","CODE"}; Cursor cursor = db.query(TABLE_NAME, result_columns, "CITY" +"=?", new String[]{"k"}, null, null, null); cursor.moveToFirst(); String xnewcode=cursor.getString(0); Toast.makeText(activity1.this, xnewcode, Toast.LENGTH_LONG).show(); cursor.moveToNext(); //db.close(); }} catch(Exception e) { Toast.makeText(activity1.this,"Fault in showing " + e,Toast.LENGTH_LONG).show(); } } }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload