Android SQLite closing database between 2 activities - android

I know there are similar forum threads to this one, but I've read them and tried the methods for solving the problem but it doesn't seem to do it.
I get the:
close() was never explicity called on database /data/data...
application did not close the database or cursor that was opened...
My error does not occur directly, it happens after awhile, when I've gone back and forth between the two activities.
I use two activities which both need a connection to the database. My idea was for the first activity to close the database before the other activity starts. Here is my code:
#Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
movies = new MoviesData(this);
cursor = getTitles();
showTitles(cursor);
}
#Override
public void onPause() {
movies.close();
super.onPause();
}
#Override
public void onResume() {
movies = new MoviesData(this);
super.onResume();
}
This is my first activity, the second one is pretty much alike and is started upon a buttonpress. The MoviesData class is just an empty SQLiteOpenHelper class found below.
public class MoviesData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "movies.db";
private static final int DATABASE_VERSION = 1;
public MoviesData(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
I now know when the error is displayed. If I quit the application and then open it again and try to do something the error is showing. I've tried to close the database in "onDestroy()" but that doesn't help either...

//
#Override
public void onPause() {
super.onPause();
movies.close();
cursor.close();
}
// no need to write this code
#Override
public void onResume() {
movies = new MoviesData(this);
super.onResume();
}

I made it work now!
I just added this to my SQLiteOpenHelper class.
public class MoviesData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "movies.db";
private static final int DATABASE_VERSION = 1;
private static MoviesData movies;
private MoviesData(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public static MoviesData getInstance(Context context) {
if (movies == null) {
movies = new MoviesData(context);
}
return movies;
}
}

Related

Opening an Sqlite database in a background thread

I am trying to better understand what it means to open an Sqlite database on a background thread in Android. Right now I am using a static/singleton pattern for my database via my class DatabaseHelper, so I only need to open it once, but I want to open it using good practice and understand why I shouldn't open it directly from within my Activity directly (or within the helper's constructor, for example).
My class:
public class DatabaseHelper extends SQLiteOpenHelper {
private static volatile SQLiteDatabase mDatabase;
private static DatabaseHelper mInstance = null;
private static Context mContext;
// ...
public static synchronized DatabaseHelper getInstance(Context context) {
/**
* use the application context as suggested by CommonsWare.
* this will ensure that you don't accidentally leak an Activity's
* context (see this article for more information:
* http://android-developers.blogspot.nl/2009/01/avoiding-memory-leaks.html)
*/
if (mInstance == null) {
mInstance = new DatabaseHelper(context.getApplicationContext());
}
return mInstance;
}
private DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
mContext = context;
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_CREATE_SOME_TABLE); //some SQL expression
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DB_ALTER);
}
public void open() throws SQLException {
mDatabase = getWritableDatabase();
}
public void close() {
mDatabase.close();
}
public boolean isOpen() {
return mDatabase.isOpen();
}
//below this would be various CRUD functions operating on mDatabase
// ...
// ...
}
Is it correct to say that you should do something like this:
DatabaseHelper mDatabaseHelper = DatabaseHelper.getInstance(this);
Thread thread = new Thread("OpenDbThread") {
public void run(){
mDatabaseHelper.open();
}
};
thread.start();
inside an Activity somewhere?
You're correct that the code you wrote would open the database on a background thread. However, you wouldn't actually the database was opened until thread.isAlive() returned false (or mDatabase.isOpen() returned true). Alternatively, you could make your Activity listen for a callback from your Thread.

A SQLiteConnection object for database Please fix

04-11 05:05:17.837: W/SQLiteConnectionPool(6454): A SQLiteConnection object for database Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
this is an error return by my logcat ive searched for the possible solution for this error in here andhere saying you should close database but i have this code
#Override
protected void onPause() {
super.onPause();
if (db.isOpen()) {
db.close();
}
}
on all my ativity so im wondering why i get this error
public class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper sInstance;
private static final String DATABASE_NAME = "database_name";
private static final String DATABASE_TABLE = "table_name";
private static final int DATABASE_VERSION = 1;
public static DatabaseHelper getInstance(Context context) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
if (sInstance == null) {
sInstance = new DatabaseHelper(context.getApplicationContext());
}
return sInstance;
}
/**
* Constructor should be private to prevent direct instantiation.
* make call to static factory method "getInstance()" instead.
*/
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
im currently doing the same thing im doing this tutorial if you want to try it you can you can find it here

How to call methods of an external class from Activity?

First let you know I am new in Android.
Trying to create multiple classes to handle database table operations. Created a database helper as follow:
public class WSDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "wsemp";
private static final int DATABASE_VERSION = 5;
public WSDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase database) {
.............
}
#Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
................
}
}
Created a class to handle database table operation:
public class CustomerBean {
private WSDatabaseHelper database;
#Override
public boolean onCreate() {
database = new WSDatabaseHelper(getContext());
return false;
}
public boolean insertObject(valObj) {
SQLiteDatabase db = database.getWritableDatabase();
db.insert(.......);
}
}
But now I am not sure how I can call this insertObject function from my activity or session file. I tried by CustomerBean.isnertObject(obj) but it's asking to change the method to static.
There are two ways to call method in this situation
Create the object of the class and call method
// Create object
CustomerBean customerBean = new CustomerBean();
// call the method
customerBean.insertObject(<insert object here>);
Make the method static and call it from class name
// In CustomerBean class
public static boolean insertObject(valObj) {
SQLiteDatabase db = database.getWritableDatabase();
db.insert(.......);
}
//In WSDatabaseHelper class
CustomerBean.insertObject(<object name here>);
On more thing to correct here is that in CustomerBean class you have written
#Override
public boolean onCreate() {
database = new WSDatabaseHelper(getContext());
return false;
}
Which is not correct. onCreate() method of Activity class of Android and
you can put #Override Annotation for this method only if your class is extending Activity class
Hope this will help you
Add the static modifier to your method. Then you should be able to access it between classes.

passing context from non-activity

i yet really grasp this whole context thing we found a lot in android programming. so i tried creating a function to drop all my tables, and here's a my partial code:
public class DBAdapter {
private static class DbHelper extends SQLiteOpenHelper {
private boolean databaseCreated = false;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void deleteTables(){
Log.d("DBAdapter","dlm drop tables pre");
this.sqlDatabase.execSQL("DROP TABLE IF EXISTS ["+TABLE_TV+"];");
this.sqlDatabase.execSQL("DROP TABLE IF EXISTS ["+TABLE_CAMERA+"];");
this.sqlDatabase.execSQL("DROP TABLE IF EXISTS ["+TABLE_GPS+"];");
}
}
}
and the part where i'm going to call the function deleteTables
public class UpdateDatabase {
public void updateTable(String table,JSONObject jsonObject){
DBAdapter db = new DBAdapter(this);
db.deleteTables();
}
}
but of course it will return an error, since DBAdapter expects a context. public class UpdateDatabase is not an activity class. Calling DbAdapter db = new DBAdapter(this) from activity class will work just find. So how do I find any fix for this problem?
thanks
You can add a constructor to UpdateDatabase that takes a Context and stores it so that it is available to be used by updateTable. Something like this:
public class UpdateDatabase {
private final Context mContext;
public UpdateDatabase(Context context){
mContext = context;
}
public void updateTable(String table,JSONObject jsonObject){
DBAdapter db = new DBAdapter(mContext);
db.deleteTables();
}
}
Now, whenever you do new UpdateDatabase() you will need to do new UpdateDatabase(..context..) instead. If you are doing this from an Activity, then you can do new UpdateDatabase(this).
hi look this code..
public class DbManager
{
// the Activity or Application that is creating an object from this class.
Context context;
CustomSQLiteOpenHelper helper;
// a reference to the database used by this application/object
protected SQLiteDatabase db;
private static DbManager INSTANCE;
// These constants are specific to the database.
protected final String DB_NAME = "yourDB";
protected final int DB_VERSION = 1;
public DbManager(Context context)
{
this.context = context;
// create or open the database
helper = new CustomSQLiteOpenHelper(context);
this.db = helper.getWritableDatabase();
}
public static DbManager getInstance(Context context){
if(INSTANCE == null)INSTANCE = new DbManager(context);
return INSTANCE;
}
public void db_Close()
{
if(helper!=null){
helper.close();
}
this.db.close();
}
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
public CustomSQLiteOpenHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
// This string is used to create the database.
// execute the query string to the database.
//db.execSQL(newTableQueryString);
Log.i("DataBaseManager", "Create Table");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION.
// OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE.
}
}
}
// Inherit the DbManager Class
public class DataCollection extends DbManager {
public DataCollection(Context context){
super(context);
}
public void deleteTable(String TABLE_NAME){
try {db.execSQL("DROP TABLE "+TABLE_NAME);}//.delete(TABLE_NAME, null, null);}
catch (Exception e){
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}

ListView SimpleCursorAdapter Updated Asynchronously

I have a class that has a SimpleCursorAdapter as a field. That adapter is used feed a listview that has a a viewBinder.
I have an asynchronous task that runs that adds an entry to the database and then updates the cursor.
In testing, if I click too quickly on the button that runs the async process, I get an error:
java.lang.RuntimeException: An error occured while executing doInBackground()
...
Caused by: java.lang.IllegalStateException: database [path_to_my_db] already closed
The code WORKS perfectly - unless... the user clicks the save button rapidly in succession... I'm new to all of this, so any input would be greatly appreciated!
Here is a stripped down version of what I'm trying to do:
public class MyActivity extends Activity {
private DatabaseConnector connector; // this is my class for managing SQLite
private SimpleCursorAdapter adapter;
....
#Override
public void onCreate(Bundle savedInstanceState){
...
myListView = (ListView)findViewById(R.id.my_list_view);
String[] = new String{"This", "part", "is", "working"};
int[] to = new int[] {1,2,3,4}; // again, this is working...
adapter = new SimpleCursorAdapter(this, R.layout.my_list_item_row, null, from, to);
adapter.setViewBinder(new ViewBinder(){
... // this is all working
... // the viewBinder is for custom date formatting... again, all works
});
myListView.setAdapter(adapter);
}
private class MyAsyncTask extends AsyncTask<Context, Void, ExerciseInstanceViewModel>{
MyViewModel vm; // this viewModel has a cursor member...
public MyAsyncTask([variables-all-working]){
}
#Override
protected MyViewModel doInBackground(Context... params) {
connector = new DatabaseConnector(MyActivity.this);
connector.open(); // TODO: Getting 'did not close database error here...'
vm = connector.runMethodThatIncludesCursorInReturnType([input-paramters-working]);
return vm;
}
// use the cursor returned from the doInBackground method
#Override
protected void onPostExecute(MyViewModel result){
super.onPostExecute(result);
// set instance fields in outer class...;
// set textView, progressBar, etc..
if (result.MyCursor != null)
{
adapter.changeCursor(result.MyCursor);
}
connector.close(); // aren't i closing the db here???
[Code to reload page with next detail items]
}
}
}
if (result.MyCursor != null)
{
adapter.changeCursor(result.MyCursor);
}
}
}
connector.close();
}
try this.
Make a wrapper around the connector, synchronized and as a singleton. I believe you are accessing the database at the same time. This is a stripped version of one of my own:
(I am overriding the getWriteable database to enable foreign keys but you dont have too do that)
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "myDatabase";
private static final int DATABASE_VERSION = 1;
private Context context = null;
private static DatabaseHelper instance = null;
public static synchronized DatabaseHelper getInstance(Context context){
if (instance == null){
instance = new DatabaseHelper(context, null);
}
return instance;
}
#Override
public synchronized SQLiteDatabase getWritableDatabase() {
SQLiteDatabase db = super.getWritableDatabase();
db.execSQL("PRAGMA foreign_keys=ON;");
return db;
}
private DatabaseHelper(Context context, CursorFactory factory) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
this.context = context;
}
}

Categories

Resources