android - How to retrieve data from sqlite file -


i doing search operation.i created dbadapter. able manually insert values , perform search. want insert data sqlite file.how proceed? should place sqlite file in assets folder? please tell me how proceed. want search data sqlite file.

here main activity code:

public void oncreate(bundle savedinstancestate)  {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);      searchview = (searchview) findviewbyid(r.id.search);     searchview.seticonifiedbydefault(false);     searchview.setonquerytextlistener(this);     searchview.setoncloselistener(this);      mlistview = (listview) findviewbyid(r.id.list);     assetmanager manager = this.getassets();     mdbhelper = new customersdbadapter(this);     mdbhelper.open();       mdbhelper.deleteallcustomers();     //add customer data sample     mdbhelper.createcustomer("pizza1", "piz", "1107 west ada", "", "los angeles");     mdbhelper.createcustomer("pizza2", "pizza hut", "1562 west ", "", "los angeles");     mdbhelper.createcustomer("sub4", "subway", "504 west ", "", "los angeles"); } 

here dbadapter:

public class customersdbadapter  {     public static final string key_rowid = "rowid";     public static final string key_customer = "customer";     public static final string key_name = "name";     public static final string key_address = "address";     public static final string key_address1 = "address1";     public static final string key_address2 = "address2";     public static final string key_city = "city";     public static final string key_search = "searchdata";      private static final string tag = "customersdbadapter";     private databasehelper mdbhelper;     private sqlitedatabase mdb;      private static final string database_name = "customerdata";     private static final string fts_virtual_table = "customerinfo";     private static final int database_version = 1;      //create fts3 virtual table fast searches     private static final string database_create =     "create virtual table " + fts_virtual_table + " using fts3(" +     key_customer + "," +     key_name + "," +     key_address1 + "," +     key_address2 + "," +     key_city + "," +     //key_state + "," +     //key_zip + "," +     key_search + "," +     " unique (" + key_customer + "));";       private final context mctx;      private static class databasehelper extends sqliteopenhelper {      databasehelper(context context) {         super(context, database_name, null, database_version);     }       @override     public void oncreate(sqlitedatabase db) {         log.w(tag, database_create);         //db.execsql(database_create);     }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         log.w(tag, "upgrading database version " + oldversion + " "                 + newversion + ", destroy old data");         db.execsql("drop table if exists " + fts_virtual_table);         oncreate(db);     } }  public customersdbadapter(context ctx)  {     this.mctx = ctx; }  public customersdbadapter open() throws sqlexception {     mdbhelper = new databasehelper(mctx);     mdb = mdbhelper.getwritabledatabase();     return this; }  public void close() {     if (mdbhelper != null) {         mdbhelper.close();     } }   public long createcustomer(string customer, string name, string address1, string address2, string city) {      contentvalues initialvalues = new contentvalues();     string searchvalue =     customer + " " +                              name + " " +                              address1 + " " +                              city + " "                            //   state + " " + zipcode                             ;     initialvalues.put(key_customer, customer);     initialvalues.put(key_name, name);     initialvalues.put(key_address1, address1);     initialvalues.put(key_address2, address2);     initialvalues.put(key_city, city);     //initialvalues.put(key_state, state);     //initialvalues.put(key_zip, zipcode);     initialvalues.put(key_search, searchvalue);      return mdb.insert(fts_virtual_table, null, initialvalues); }   public cursor searchcustomer(string inputtext) throws sqlexception {     log.w(tag, inputtext);     string query = "select docid _id," +      key_customer + "," +     key_name + "," +     "(" + key_address1 + "||" +      "(case when " + key_address2 +  "> '' '\n' || " + key_address2 + " else '' end)) " +  key_address +"," +     key_address1 + "," +     key_address2 + "," +     key_city +       //key_zip +     " " + fts_virtual_table +     " " +  key_search + " match '" + inputtext + "';";     log.w(tag, query);     cursor mcursor = mdb.rawquery(query,null);      if (mcursor != null) {         mcursor.movetofirst();     }     return mcursor;  }     } 

should place sqlite file in assets folder?

=> there 2 ways manage database in android.

  1. create fresh database through code
  2. use existing database

create fresh database through code:

in method, have create database , tables through code only. wouldn't suggest method novice programmer there chances of mistakes in code of sqlite operations.

here homework: android database example

use existing database:

i suggest go 2nd method easy, in can create database using gui tool sqlite manager, can download tools in mozilla firefox browser.

create database required tables on there , place database file in assets folder.

now, have write code use existing database. there plenty of examples available same on web.

here homework: use existing database in android


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -