sqlite - database is not created in android -


in application activity running fine database not getting created there no error in logcat. main activity class:

package com.example.testdb;  import android.os.bundle; import android.app.activity; import android.view.menu;  public class mainactivity extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         database d=new database(this);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.main, menu);         return true;     } } 

database class:

package com.example.testdb;  import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqlitedatabase.cursorfactory; import android.database.sqlite.sqliteopenhelper; import android.widget.toast;  public class database extends sqliteopenhelper{      string tablename = "table1";     private string column1 = "regionid";     private string column2 = "regionname";     private string column3 = "currency";     sqlitedatabase db;      public database(context context) {         super(context, "test", null, 2);         this.getwritabledatabase();         // todo auto-generated constructor stub     }      @override     public void oncreate(sqlitedatabase db) {         // todo auto-generated method stub         try {             final string r_table = "create table " + tablename + " (" + column1+ " integer primary key , " + column2 + " text, " + column3 + " text) ";             db.execsql(r_table);             contentvalues cv = new contentvalues();             cv.put(column1, 1);             cv.put(column2, "india");             cv.put(column3, "rupee");             db.insert(r_table, null, cv);         }         catch(exception e){             system.out.println(e.getmessage());         }          cursor c = db.rawquery("select * table1", null);          if (c != null ) {             if  (c.movetofirst()) {                 //toast.maketext(new mainactivity().getapplicationcontext(), c.getcount(), toast.length_long).show();                 system.out.println("rows are:"+c.getcount());             }         }         c.close();       }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         // todo auto-generated method stub     } } 

logcat:

05-18 12:45:22.042: i/activitythread(3270): switching default density 160 130 05-18 12:47:18.162: i/activitythread(3415): switching default density 160 130 

i using virtual box emulator , in ddms can't see database got created.

can please me in this.

thanks in advance siva

according documentation sqliteopenhelper uses lazy model of creating databases. means, in absence of database, oncreate called not in constructor, when database needed, i.e. @ first call getreadabledatabase or getwritabledatabase. so, situation normal.

think of sqliteopenhelper provider of connection database, containing additional logic handling situations "if database not exist" , "if database outdated". thus, in case, don't worry when database file created.

database usage in activity (if one, using db) like:

private sqliteopenhelper connection;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     connection = new database(this); //no actual db creation here     //if want test db table creation without further work uncomment following line     //sqlitedatabase db = connection.getreadabledatabase(); }  public void onbtnaddrecordclick(view view) {     sqlitedatabase db = connection.getwritabledatabase(); //if not right                                                   //the db corrected here     contentvalues values;     //... fill in record insert     db.insert(my_table_name, null, values); } 

important note: if going access db multiple activities, consider making database extending sqlitedatabasehelper singleton or wrap contentprovider, because frowned upon use multiple connections database without clear purpose.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -