Close () was not called by a database call

I need to know where to call db.close () in my code. I added it to the onCreate () method, but when I need to use some methods, it says that the database is not open, and then I deleted it from onCreate (), and says that the close () function was not called by the explication. so where should I close, can it be inside every class method?

here is the code:

public class HoursPerDayDataHelper {

private static final String DATABASE_NAME = "database.db";
private static final int DATABASE_VERSION = 1;
protected static final String TABLE_NAME = "table";
protected String TAG = "HoursPerDayDataHelper";

private Context context;
private SQLiteDatabase db;
OpenHelper openHelper = null;

public HoursPerDayDataHelper(Context context) {
    this.context = context;
    openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
    openHelper.onCreate(db);

}

public void close() {

    if (openHelper != null) {
        openHelper.close();
    }

}

public void deleteAll() {
    this.db.delete(TABLE_NAME, null, null);
}

public String selectDuration(String date) {

    String duration = "";
    Integer value = 0;
    String returnment = "";
    Log.i(TAG, "date do select: " + date);              

    Cursor cursor = this.db.query(TABLE_NAME, new String[] { "duration" },
            "date = ? ", new String[]{ date }, null, null, null);

    Log.i(TAG, "cursor string " + cursor);

    if (cursor.moveToFirst()) {
        do {
            Log.i(TAG, "dentro do if cursor");
            duration = cursor.getString(0);
            value += Integer.parseInt(duration);

        } while (cursor.moveToNext());
        returnment = Integer.toString(value);
    }else{

        Log.i(TAG, "bla bla bla");

    }

    if (cursor != null && !cursor.isClosed()) {
        cursor.close();

    }
    return returnment;
}

public ArrayList<String[]> selectTopContacts() {

    ArrayList<String[]> list1 = new ArrayList<String[]>();
    Cursor cursor = this.db.query(TABLE_NAME, null, null, null, null, null,
            "duration desc");

    if (cursor.moveToFirst()) {
        do {
            if (cursor.getString(2) != "") {

                String[] data = new String[4];
                data[0] = cursor.getString(2);
                data[1] = cursor.getString(4);
                data[2] = cursor.getString(5);
                data[3] = cursor.getString(7);

                list1.add(data);

            } else {

                String[] data = new String[3];
                data[1] = cursor.getString(4);
                data[2] = cursor.getString(5);
                data[3] = cursor.getString(7);

                list1.add(data);

            }
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();

    }
    return list1;
}

public static class OpenHelper extends SQLiteOpenHelper {

    OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS "
                + TABLE_NAME
                + "(id INTEGER PRIMARY KEY AUTOINCREMENT, duration TIME, date DATE, current_time TIME)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("HoursPerDay Database",
                "Upgrading database, this will drop tables and recreate.");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

}

And this is my activity:

public class HoursPerDay extends Activity{

private String LOG_TAG = "HoursPerDay";
private TextView mDateDisplay;
public String date;
private int mYear;
private int mMonth;
private int mDay;
private int newDay;
private String hpdData; 
private HoursPerDayDataHelper hpd;

OpenHelper openHelper = new OpenHelper(HoursPerDay.this);

static final int DATE_DIALOG_ID = 0;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hours_per_day);

    hpd = new HoursPerDayDataHelper(this);

    // capture our View elements
    mDateDisplay = (TextView) findViewById(R.id.dateDisplay);   

    // get the current date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    // display the current date (this method is below)
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (openHelper != null) {
        openHelper.close();
    }
    if (hpd != null) {
        hpd.close();
    }
}

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {

        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;                  

        setBasicContent();

        hpd.close();


    }
};

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
    }
    return null;
}

@Override
public boolean onCreateOptionsMenu(Menu menu){

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.layout.hoursperdaymenu, menu);
    return true;

}

@Override
public boolean onOptionsItemSelected(MenuItem item){

    switch(item.getItemId()){

    case R.id.filter_by_day:            
            showDialog(DATE_DIALOG_ID);
            return true;
    case R.id.filter_by_user:

        showDialog(DATE_DIALOG_ID);
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }

}   

public void setBasicContent() {

    date = (mMonth + 1) + "/" + newDay + "/" + mYear;       
    hpdData = this.hpd.selectDuration(date);
    mDateDisplay.setText(hpdData);
    hpd.close();
}

}
+2
source share
2 answers

I think there are two ways:

  • in- onPausemethod and check there isFinishingif yes β†’ close. Problem: if your application is killed by the killer application, db remains open.
  • Each time you open and close the databases (methods) that you read / write.

EDIT:
, , . , SQLiteOpenHelper. onCreate.
, - DBHelper , SQLDataHandler.
. , , . :

, Helper:

public static class OpenHelper extends SQLiteOpenHelper {

 private static final String DATABASE_NAME = "database.db";
 private static final int DATABASE_VERSION = 1;
 protected static final String TABLE_NAME = "table";
 protected String TAG = "HoursPerDayDataHelper";

CREATE TABLE /, .
, ,

 @Override
 public void onCreate(SQLiteDatabase db) {

    String query = "CREATE TABLE "
               + TABLE_NAME
               + "(id INTEGER PRIMARY KEY AUTOINCREMENT, duration TIME, date DATE,          current_time TIME)";
    db.execSQL(query);
    }
 }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

 OpenHelper(Context context) {
     super(context, DATABASE_NAME, null, DATABASE_VERSION);
 }
}

:
DataHandler :

OpenHelper helper = new OpenHelper(ctx);
// SQLiteDatabase db = helper.getReadableDatabase();
SQLiteDatabase db = helper.getWritableDatabase();

, , .., "DataHandler".
, . , , DataHandler class db.close().
, DB . , ;)

, . :)


EDIT2:

-, .
: , . . ( smal).
, OpenHelper. . , : DataHandlerDB.

:

package ...;

import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DataHandlerDB {

  public static void persistAll(Context ctx, List<Module> moduleList) {

      DatabaseHelper helper = new DatabaseHelper(ctx);

      SQLiteDatabase db = helper.getWritableDatabase();

      ContentValues values = new ContentValues();
      for (Module m : moduleList) {

          values.put("_id", m.get_id());
          values.put("name", m.getModule());

          db.insert("module", null, values);
      }
      db.close();
  }

  public static List<Module> findAll(Context ctx) {

      List<Module> result = new ArrayList<Module>();
      DatabaseHelper helper = new DatabaseHelper(ctx);
      SQLiteDatabase db = helper.getReadableDatabase();

      Cursor c = db.query(ModuleDB.TABLE_NAME, new String[] { ModuleDB.ID,
            ModuleDB.MODULE}, null, null, null, null, null);

      while (c.moveToNext()) {
          Module m = new Module(c.getInt(0), c.getString(1));
          result.add(m);
      }
      c.close();
      db.close();

      return result;
  }

  // Update Database entry
  public static void update(Context ctx, Module m) {

      DatabaseHelper helper = new DatabaseHelper(ctx);
      SQLiteDatabase db = helper.getWritableDatabase();
      ContentValues values = new ContentValues();

      values.put("_id", m.get_id());
      values.put("name", m.getModule());

      db.update("module", values, null, null);
      db.close();
  }

  public static void delete(Context ctx, Module m) {

      DatabaseHelper helper = new DatabaseHelper(ctx);
      SQLiteDatabase db = helper.getWritableDatabase();
      ContentValues values = new ContentValues();

      values.put("_id", m.get_id());
      values.put("name", m.getModule());

      db.delete("module","_id = m.get_id()", null);
      db.close();
  }

  public static void createDB(Context ctx) {
      DatabaseHelper helper = new DatabaseHelper(ctx);
      SQLiteDatabase db = helper.getWritableDatabase();
      db.close();
  }
}

, static, .
:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            // get the a writable DB, in case it not existing it gets created.
    DataHandlerDB.createDB(this);
    // get stuff out of DB
    moduleList = DataHandlerDB.findAll(this);

    adapter = new ArrayAdapter<Module>(this,
            android.R.layout.simple_list_item_1, moduleList);
    setListAdapter(adapter);
}
+2

onCreate, onDestroy

0

Source: https://habr.com/ru/post/1791922/


All Articles