When I'm trying to run my project in android 2.x and execute sqlcommands on the database, an error says that "close() was never explicitly called on database...". but it works fine in android 4.x. I think I should open and close database in my dbhelper but I don't know how to do it in this file. This is my DbHelper:
public class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, "shareholders.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
// this.db = db;
String sql = "CREATE TABLE IF NOT EXISTS news (id integer,title text,description text,sDate text)";
db.execSQL(sql);
sql = "CREATE TABLE IF NOT EXISTS cities (id integer,name text)";
db.execSQL(sql);
sql = "CREATE TABLE IF NOT EXISTS lawyers (id integer,fullName text,mobile text)";
db.execSQL(sql);
sql = "CREATE TABLE IF NOT EXISTS persons (id integer,code text,fullName text,father text,birthDate text,shsh text,nationalCode text,city text,postalCode text,email text,homeTel text,workTel text,mobile text,homeAddress text,workAddress text)";
db.execSQL(sql);
sql = "CREATE TABLE IF NOT EXISTS settings (name text,value text)";
db.execSQL(sql);
sql = "CREATE TABLE IF NOT EXISTS messages (id integer,senderId integer,receiverId integer,SenderName text,sDate text,type text,title text, body text,openned text,sent integer)";
db.execSQL(sql);
sql = "CREATE TABLE IF NOT EXISTS financials (id integer,sDate text,mDate text,description text,price integer)";
db.execSQL(sql);
sql = "CREATE TABLE IF NOT EXISTS sellRequests (id integer,sDate text,mDate text,description text,shareCount integer)";
db.execSQL(sql);
sql = "CREATE TABLE IF NOT EXISTS buyRequests (id integer,sDate text,mDate text,description text,shareCount integer)";
db.execSQL(sql);
sql = "CREATE TABLE IF NOT EXISTS shares (id text,type text,sDate text,status text,count integer)";
db.execSQL(sql);
sql = "CREATE TABLE IF NOT EXISTS android_metadata (locale text)";
db.execSQL(sql);
} catch (Exception e) {
xLog.error(e.getMessage());
}
ContentValues cv = new ContentValues();
cv.put("name", "Username");
cv.put("value", "default");
db.insert("settings", null, cv);
cv.clear();
cv.put("name", "Password");
cv.put("value", "default");
db.insert("settings", null, cv);
cv.clear();
cv.put("name", "PersonId");
cv.put("value", "default");
db.insert("settings", null, cv);
cv.clear();
cv.put("name", "picture");
cv.put("value", "");
db.insert("settings", null, cv);
cv.clear();
cv.put("locale", "en_US");
db.insert("android_metadata", null, cv);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public long insert(String table,ContentValues cv){
mydb =this.getWritableDatabase();
long result=-1;
try {
result = mydb.insert(table,null, cv);
}catch (Exception e) {
xLog.error(e.getMessage());
}
finally{
// mydb.close();
}
return result;
}
public Cursor selectAll(String table){
mydb =this.getReadableDatabase();
String sql = "SELECT * FROM "+table;
xLog.info(sql);
Cursor result=null;
try {
result = mydb.rawQuery(sql, null);
} catch (Exception e) {
xLog.error(e.getMessage());
}
finally{
// result.close();
// mydb.close();
}
return result;
}
public Cursor select(String table,String where){
mydb =this.getReadableDatabase();
String sql = "SELECT * FROM "+table+" WHERE "+where;
xLog.info(sql);
Cursor result=null;
try {
result = mydb.rawQuery("SELECT * FROM "+table+" WHERE "+where, null);
} catch (Exception e) {
xLog.error(e.getMessage());
}
finally{
// result.close();
// mydb.close();
}
return result;
}
public long delete(String table,String condition){
mydb =this.getWritableDatabase();
long result = -1;
try {
result = mydb.delete(table, condition, null);
} catch (Exception e) {
xLog.error(e.getMessage());
}
finally{
// mydb.close();
}
return result;
}
protected long empty(String table){
mydb =this.getWritableDatabase();
long result = -1;
try {
result = mydb.delete(table, "", null);
} catch (Exception e) {
xLog.error(e.getMessage());
}
finally{
// mydb.close();
}
return result;
}
public long update(String table,ContentValues cv,String condition){
mydb =this.getWritableDatabase();
long result = -1;
try {
result = mydb.update(table, cv, condition, null);
} catch (Exception e) {
xLog.error(e.getMessage());
}
finally{
// mydb.close();
}
return result;
}
protected void drop(String table){
//TODO Produces a damn error!
mydb =this.getWritableDatabase();
try {
mydb.execSQL("DROP TABLE IF EXISTS "+table);
} catch (Exception e) {
xLog.error(e.getMessage());
}
finally{
}
}
}
This is my log cat:
11-28 08:04:31.663: E/Database(9025): close() was never explicitly called on database '/data/data/com.example.shareholders/databases/shareholders.db'
11-28 08:04:31.663: E/Database(9025): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
11-28 08:04:31.663: E/Database(9025): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
11-28 08:04:31.663: E/Database(9025): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
11-28 08:04:31.663: E/Database(9025): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
11-28 08:04:31.663: E/Database(9025): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
11-28 08:04:31.663: E/Database(9025): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
11-28 08:04:31.663: E/Database(9025): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
11-28 08:04:31.663: E/Database(9025): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
11-28 08:04:31.663: E/Database(9025): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
11-28 08:04:31.663: E/Database(9025): at ClassLibrary.DbHelper.select(DbHelper.java:141)
11-28 08:04:31.663: E/Database(9025): at com.example.shareholders.entities.Settings.select(Settings.java:97)
11-28 08:04:31.663: E/Database(9025): at com.example.shareholders.entities.Settings.getValue(Settings.java:58)
11-28 08:04:31.663: E/Database(9025): at com.example.shareholders.Login.onCreate(Login.java:32)
11-28 08:04:31.663: E/Database(9025): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-28 08:04:31.663: E/Database(9025): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-28 08:04:31.663: E/Database(9025): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-28 08:04:31.663: E/Database(9025): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-28 08:04:31.663: E/Database(9025): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-28 08:04:31.663: E/Database(9025): at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 08:04:31.663: E/Database(9025): at android.os.Looper.loop(Looper.java:123)
11-28 08:04:31.663: E/Database(9025): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-28 08:04:31.663: E/Database(9025): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 08:04:31.663: E/Database(9025): at java.lang.reflect.Method.invoke(Method.java:521)
11-28 08:04:31.663: E/Database(9025): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-28 08:04:31.663: E/Database(9025): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-28 08:04:31.663: E/Database(9025): at dalvik.system.NativeStart.main(Native Method)