close() was never explicitly called on database
close the database when in onDestroy
of your activity
protected void onDestroy()
{
super.onDestroy();
if (db != null)
{
db.close();
}
}
and also add close method in datahelper class as
public void close() {
// NOTE: openHelper must now be a member of CallDataHelper;
// you currently have it as a local in your constructor
if (openHelper != null) {
openHelper.close();
}
}
close() was never explicitly called on database '/data
So after seeing your code I am going to suggest a different approach that the first one where I said that you should close()
after every call.
I see a number of problems in your code and here are some suggestions:
- Why are u doing
dbhelper = new MyDbHelper(this);
multiple times? You should only do this once inonCreate
- Also you can create a member variable
SQLiteDatabase db
and just dodb= dbhelper.getWritableDatabase();
once in theonCreate
. - Remove all other calls to
db.close()
except from theonDestroy()
method. - Remove all calls to
dbhelper.getReadableDatabase();
anddbhelper.getWritableDatabase();
since you already have an instancedb
from theonCreate
method which you can use from anywhere in your activity
So basically what I am trying to say is that you just get the writable
instance of database once in the onCreate
and then close it in the onDestroy
.
Android error - close() was never explicitly called on database
I think the problem is that you need to close the db when your activity is destroyed. Try adding this to your activity:
@Override
protected void onDestroy() {
super.onDestroy();
if (openHelper != null) {
openHelper.close();
}
if (cdh != null) {
cdh.close();
}
}
and add this to your CallDataHelper class:
public void close() {
// NOTE: openHelper must now be a member of CallDataHelper;
// you currently have it as a local in your constructor
if (openHelper != null) {
openHelper.close();
}
}
EDIT 2: Also change the CallDataHelper constructor to:
// Declare openHelper as a member variable
OpenHelper openHelper = null;
public CallDataHelper(Context context) {
this.context = context;
openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
}
This should successfully close the database connection that was created by both of your OpenHelper
instances.
( EDITED to reflect the fact that you use two OpenHelper instances.)
SQLite ERROR close() was never explicitly called on database
Write the below code in your database file
public class DataBaseHelper extends SQLiteOpenHelper {
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg0) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
and then call from activity
myDataBase.close();
This is good link to refer
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
Close() was never explicitly called on database even after implementing close()
You need to design your SQLiteOpenHelper
class correctly. Many gurus suggest to make a static reference which will ensure that there exists only one DatabaseHelper
instance at any time.
Below code will give you some idea about it. (This is not a complete code but will give you a some hint)
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper mDBHelper;
public static DatabaseHelper getInstance(Context ctx) {
if (mDBHelper == null) { //this will ensure no multiple instances out there.
mDBHelper = new DatabaseHelper(ctx.getApplicationContext());
}
return mDBHelper;
}
}
Now, do a trick.
open the database
private SQLiteDatabase db;
public synchronized SQLiteDatabase open() throws SQLException {
if(db ==null){
db = DBHelper.getWritableDatabase();
}
return db;
}
Close the database.
public synchronized void close() {
//do nothing. This is a trick.
}
Reason : For mobile application, there is no need of open and close mechanism. It is best suitable for the Web application where multiple users are trying to access db but in the case of mobile application, only single user is going to use your application.
This hack will also make sure that you will never ever get the Close() was never explicitly called on database
error. This exception is thrown when you have opened more SQLiteDatabase instances than you have closed and now in our case there is only one SQLiteDatabse instance. This will also handle the concurrent db access/write problem.
Hope this hack will help you to understand the actual problem.
For better understanding, you can refer this link.
Close() was never explicitly called on database in android
Try calling SQLiteDatabase.close()
at the end of your try
in the checkDatabase
method.
close() was never explicitly called on database,android database not closed
Natto,
I've changed your code as follows, it worked for me.
Please check and let me know.
SQLiteDatabase db=openOrCreateDatabase("data",MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS location(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME VARCHAR(100),CBcode VARCHAR(100));");
Cursor c1 = db.rawQuery("Select NAME from location", null);
Cursor c2 = db.rawQuery("Select CBcode from location", null);
if(c1==null || c2 == null)
{
Toast.makeText(getBaseContext(), "Database values empty, please refer Help option befor you start using the app..", Toast.LENGTH_LONG).show();
}
else
{
c1.moveToFirst();
c2.moveToFirst();
int col1=c1.getColumnIndex("NAME");
int col2=c2.getColumnIndex("CBcode");
while(c1.moveToNext() && c2.moveToNext())
{
if(loc_name.equals(c1.getString(col1)))
{
my_cb = c2.getString(col2);
Toast.makeText(getBaseContext(), "alarm has been set", Toast.LENGTH_LONG).show();
}
else
{
// Toast.makeText(getBaseContext(), "No such values in database, please read Help carefully before using lovisis!", Toast.LENGTH_LONG).show();
}
}
}
c2.close();
c1.close();
db.close();
Your mistakes are,
1. two for loops are expensive, use a while with && as shown.
2. Close c1 and c2 also.
close() was never explicitly called on database
You need to close your cursors once you are done with them. Use try/finally
in getAllSms()
and getSmsById()
to close the cursor after you read the data.
how to solve the close() was never explicitly called on database error
Did you try putting the close()
call in a finally
block with a try/catch
?
public int count_tips() {
try {
... // your code here
}
catch (Exception e) {
// handle the exception properly
}
finally {
cursor.close()
}
}
close() was never explicitly called on database after database is imported
You need to close the database if you open it or if you are handling it globally then you don't have to open it again and again. But closing database would be a better option when not used in the activity anymore.
Change the method
private void startApp() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
to this
private void startApp() {
myhelper.close();
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
Related Topics
How to Repeat a Method Every 10 Minutes After a Button Press and End It on Another Button Press
Getintent() Extras Always Null
List of Files in Assets Folder and Its Subfolders
Determining If an Android Device Is Rooted Programmatically
How to Display a Route Between Two Geocoords in Google Maps
How to Set Shadow to a View in Android
How to Get the Position Selected in a Recyclerview
Android Slidingdrawer from Top
Android:Fill Spinner from Java Code Programmatically
Android: Webview Inside Dialog or Popup
Android: How to Use Webcam in Emulator
Example: Android Bi-Directional Network Socket Using Asynctask
Overlay Images Onto Camera Preview Surfaceview
Scroll Webview Horizontally Inside a Viewpager
Custom Seekbar (Thumb Size, Color and Background)
Jar Mismatch! Fix Your Dependencies
Android: Can Height of Slidingdrawer Be Set with Wrap_Content