Search bar with SQLite Databasee

  • Replies:6
Karan Bir Urao
  • Forum posts: 4

May 7, 2017, 10:25:47 AM via Website

Hello, I am very much in need of help. I created a database using SQLite and want to access the data of database using a search interface. So how do i go about it for making the search bar to pull data from SQLite database.
Thanks in Advance.

Reply
James Watson
  • Forum posts: 1,584

May 8, 2017, 4:03:44 AM via Website

I think you maybe need to match some fields of some tables ( or views ) of your database.
For example, your SQL command is perhaps some like ' select * from your_table where your_field like keywords'.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
Karan Bir Urao
  • Forum posts: 4

May 8, 2017, 6:15:47 AM via Website

Yeah, but the thing is that we need to create method/class which can pull data from sq-lite database and show in search bar as I click and search. As per my research it says it needs some Cursor adapter but I am not able to go on how to do it.

Reply
James Watson
  • Forum posts: 1,584

May 8, 2017, 10:43:04 AM via Website

strSQL = "select field1, field2 from table1 where key_field like '%" + strKeyword + "' ";
Cursor result = db.rawQuery(strSQL, null);

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
Karan Bir Urao
  • Forum posts: 4

May 8, 2017, 11:07:08 AM via Website

Thank you very much. This is code.. So the point where I have commented gives error (unfortunately app stops), when the comment is removed.

public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
ListView lv;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.ListView2);
myDb = new DatabaseHelper(this);
db=openOrCreateDatabase("BSR_DB",MODE_PRIVATE,null);

    //show item name on ListView

/* Cursor c = db.rawQuery("select Name from bsr_civil",null);
ArrayList ar = new ArrayList();
while(c.moveToNext())
{
ar.add(c.getString(0)); //Some kind of error from here
}

    ArrayAdapter ad = new ArrayAdapter(this,android.R.layout.simple_list_item_1,ar);   */
    lv.setAdapter(ad);                                                                
    // select item and show data of selected item on Alert Dialog
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {                   
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
           Cursor cr=db.rawQuery("SELECT * FROM bsr_civil WHERE NAME='"+lv.getItemAtPosition(position).toString()+"'",null);
            StringBuffer sb = new StringBuffer();
            while(cr.moveToNext())
            {
                sb.append(cr.getString(0)+"\n");
                sb.append(cr.getString(1)+"\n");
                sb.append(cr.getString(2)+"\n");
                sb.append(cr.getString(3)+"\n");
                sb.append(cr.getString(4)+"\n");
                sb.append(cr.getString(5)+"\n");
                sb.append(cr.getString(6)+"\n");
                sb.append(cr.getString(7)+"\n");
                sb.append(cr.getString(8)+"\n");
            }
            AlertDialog.Builder al = new AlertDialog.Builder(MainActivity.this);     
            al.setTitle("Civil Items");
            al.setMessage(sb.toString());
            al.show();

        }
    });

Reply
James Watson
  • Forum posts: 1,584

May 9, 2017, 4:48:17 AM via Website

You should try and catch any exceptions. Check whether the field value is DBNull before calling getString().

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
Karan Bir Urao
  • Forum posts: 4

May 20, 2017, 2:48:01 AM via Website

Thank You very much, i am getting into an conclusion. So now am unable to pass multiple data to the display interface. I can pass only two. So how do I pass multiple data from sqlite databasee.

 public EntryObject getEntryById(int entryId){

    EntryObject entryObject = null;
    String query = "select * from civil where _id = " + entryId;
    Cursor cursor = this.getDbConnection().rawQuery(query, null);
    if(cursor.moveToFirst()){
        do{
            String code = cursor.getString(cursor.getColumnIndexOrThrow("CODE"));
            String name = cursor.getString(cursor.getColumnIndexOrThrow("NAME"));

            entryObject = new EntryObject(code, name);

        }while(cursor.moveToNext());
    }
    cursor.close();
    return entryObject;

}

Thanks in Advance.

Reply