java - How to end transactions in progress properly and to close the database in Android? -


i develop simple application in android. execute time stopped unfortunately , throw error these

a sqliteconnection object database '/data/data/com.h2o/databases/mydbname.db' leaked! please fix application end transactions in progress , close database when no longer needed.

what avoid above error

here db code:

public class dbhelper extends sqliteopenhelper {      public static final string database_name = "mydbname.db";     public static final string address_table_name = "address";     public static final string address_column_id = "id";     public static final string address_column_addresstype = "addrtype";     public static final string address_column_line1 = "line1";     public static final string address_column_line2 = "line2";     public static final string address_column_city = "city";     public static final string address_column_zipcode = "zipcode";     public static final string address_column_country = "country";     public static final string address_column_state = "state";     public static final string address_column_landmark = "landmark";        //private hashmap hp;      public dbhelper(context context)     {         super(context, database_name, null, 1);     }      @override     public void oncreate(sqlitedatabase db) {         // todo auto-generated method stub         db.execsql(                 "create table address " +                         "(id integer primary key autoincrement,addrtype text not null unique, line1 text not null,line2 text not null,city text not null,zipcode text not null, country textnot null,state text not null,landmark text)"         );        }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         // todo auto-generated method stub         db.execsql("drop table if exists address");          oncreate(db);      }      public boolean insertaddress(string addrtype, string line1, string line2, string city, string zipcode, string country, string state, string landmark)     {          sqlitedatabase db;          db= this.getwritabledatabase();         contentvalues contentvalues = new contentvalues();         contentvalues.put("addrtype", addrtype);         contentvalues.put("line1", line1);         contentvalues.put("line2", line2);         contentvalues.put("city", city);         contentvalues.put("zipcode", zipcode);         contentvalues.put("country", country);         contentvalues.put("state", state);         contentvalues.put("landmark", landmark);          string selectquery = "select * address addrtype ='"+addrtype+"'";         cursor cursor = db.rawquery(selectquery,null);          if (cursor.getcount()==1){              db.update("address",contentvalues,"addrtype ='"+addrtype+"'",null);             return true;         }         else {              db.insert("address", null, contentvalues);             return true;         }      }       public cursor getdata(int id){         sqlitedatabase db = this.getreadabledatabase();         cursor res =  db.rawquery( "select * address id="+id+"", null );         return res;     }      public int numberofrows(){         sqlitedatabase db = this.getreadabledatabase();         int numrows = (int) databaseutils.querynumentries(db, address_table_name);         return numrows;     }      public boolean updateaddress (integer id,string addrtype, string line1, string line2, string city, string zipcode,string country,string state,string landmark)     {         sqlitedatabase db = this.getwritabledatabase();         contentvalues contentvalues = new contentvalues();         contentvalues.put("addrtype", addrtype);         contentvalues.put("line1", line1);         contentvalues.put("line2", line2);         contentvalues.put("city", city);         contentvalues.put("zipcode", zipcode);         contentvalues.put("country", country);         contentvalues.put("state", state);         contentvalues.put("landmark", landmark);         db.update("address", contentvalues, "id = ? ", new string[] { integer.tostring(id) } );         return true;     }      public integer deleteaddress (integer id)     {         sqlitedatabase db = this.getwritabledatabase();         return db.delete("address",                 "id = ? ", new string[] { integer.tostring(id) });     }      public arraylist<string> getalladdress()     {         arraylist<string> array_list = new arraylist<string>();          //hp = new hashmap();         sqlitedatabase db = this.getreadabledatabase();         cursor res =  db.rawquery( "select * address", null );         res.movetofirst();          while(res.isafterlast() == false){             array_list.add(res.getstring(res.getcolumnindex(address_column_addresstype)));             res.movetonext();         }         return array_list;     }    } 

addressfragment.java:

mydb = new dbhelper(this.getactivity());  cursor rs = mydb.getdata(value);                 id_to_update = value;                  rs.movetofirst();                 string addr_type = rs.getstring(rs.getcolumnindex(dbhelper.address_column_addresstype));                 string addr_line1 = rs.getstring(rs.getcolumnindex(dbhelper.address_column_line1));                 string addr_line2 = rs.getstring(rs.getcolumnindex(dbhelper.address_column_line2));                 string addr_city = rs.getstring(rs.getcolumnindex(dbhelper.address_column_city));                 string addr_zipcode = rs.getstring(rs.getcolumnindex(dbhelper.address_column_zipcode));                 addr_country_index = rs.getstring(rs.getcolumnindex(dbhelper.address_column_country));                 addr_state_index = rs.getstring(rs.getcolumnindex(dbhelper.address_column_state));                 string addr_landmark = rs.getstring(rs.getcolumnindex(dbhelper.address_column_landmark));                  if (!rs.isclosed())                 {                     rs.close();                     mydb.close();                 } 

please fix it!

thanks in advance

i think getting error because not closing 'cursor' object. each cursor should closed when you're finished it. safe way is:

cursor res =  db.rawquery( "select * address", null);     try {         // read data cursor in here     } {         res.close();     } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -