1

I am counting the data from data base (element ).But I am getting exception on this line Here is my log.

Here is my main java file here there is two save ok log which is printed then exception is display on count line

package com.example.database_example;

    import java.util.List;

    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;

    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Information inf=new Information("naveen");
            Information inf2=new Information("Rvi");
            DataBaseExample dbx=new DataBaseExample(MainActivity.this);

            if(dbx.insertname(inf)){
                Log.v("checkdbx.insertname(inf);", "save ok.");
            }else{
                Log.v("checkdbx.insertname(inf);", "save failed.");
            }

            if(dbx.insertname(inf2)){
                Log.v("checkdbx.insertname(inf);", "save ok.");
            }else{
                Log.v("checkdbx.insertname(inf);", "save failed.");
            }
            Log.v("count", "mane ok."+dbx.getContactsCount());


            List<Information> itemsList= dbx.getAllItems();
            for (int i = 0; i < itemsList.size(); i++) {


            Information inft= itemsList.get(i);
            Log.v("name", "mane ok."+inft.getName()+inft.getKey());


            }


        }           

    }

And the error below..

09-24 09:38:51.288: V/checkdbx.insertname(inf);(674): save ok.
09-24 09:38:51.318: V/checkdbx.insertname(inf);(674): save ok.
09-24 09:38:51.328: D/AndroidRuntime(674): Shutting down VM
09-24 09:38:51.328: W/dalvikvm(674): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-24 09:38:51.348: E/AndroidRuntime(674): FATAL EXCEPTION: main
09-24 09:38:51.348: E/AndroidRuntime(674): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database_example/com.example.database_example.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT  * FROM Name) 
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.os.Looper.loop(Looper.java:123)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-24 09:38:51.348: E/AndroidRuntime(674):  at java.lang.reflect.Method.invokeNative(Native Method)
09-24 09:38:51.348: E/AndroidRuntime(674):  at java.lang.reflect.Method.invoke(Method.java:507)
09-24 09:38:51.348: E/AndroidRuntime(674):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-24 09:38:51.348: E/AndroidRuntime(674):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-24 09:38:51.348: E/AndroidRuntime(674):  at dalvik.system.NativeStart.main(Native Method)
09-24 09:38:51.348: E/AndroidRuntime(674): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT  * FROM Name) 
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:67)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:287)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:268)
09-24 09:38:51.348: E/AndroidRuntime(674):  at com.example.database_example.DataBaseExample.getContactsCount(DataBaseExample.java:98)
09-24 09:38:51.348: E/AndroidRuntime(674):  at com.example.database_example.MainActivity.onCreate(MainActivity.java:32)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-24 09:38:51.348: E/AndroidRuntime(674):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-24 09:38:51.348: E/AndroidRuntime(674):  ... 11 more


package com.example.database_example;

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;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseExample extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "information";

    // Contacts table name
    private static final String TABLE_Name= "Name";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";

    public DataBaseExample(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String createTable= "CREATE TABLE " + TABLE_Name+"("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT"
                + ")";
        db.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
         db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name);

            // Create tables again
            onCreate(db);
    }

    public boolean insertname(Information information) {

        boolean createSuccessful = false;

        ContentValues values = new ContentValues();

      //  values.put(KEY_ID, information.getId());
        values.put(KEY_NAME, information.getName());

        SQLiteDatabase db = this.getWritableDatabase();

        createSuccessful = db.insert(TABLE_Name, null, values) > 0;
        db.close();

        return createSuccessful;
    }

    public List<Information> getAllItems(){
    List<Information> itemsList = new ArrayList<Information>();
    Cursor cursor = null;
    try {
        //get all rows
        SQLiteDatabase mDatabase = this.getReadableDatabase();
        cursor = mDatabase.query(TABLE_Name, null, null, null, null,
                null, null);
        if (cursor.moveToFirst()) {
            do {
                Information c = new Information();
                c.setName(cursor.getString(cursor.getColumnIndex((KEY_NAME))));
                c.setKey(cursor.getString(cursor.getColumnIndex((KEY_ID))));
                itemsList.add(c);
            } while (cursor.moveToNext());
        }
    } catch (SQLiteException e) {
        e.printStackTrace();
    } finally {
        cursor.close();
    }
    return itemsList;
}

    public int getContactsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_Name;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

}

package com.example.database_example;

import android.R.string;

public class Information {

    String name;

    String key;
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public Information(String name) {
        // TODO Auto-generated constructor stub
        this.name=name;
    }
    public Information() {
        // TODO Auto-generated constructor stub

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
3
  • 1
    Where is your getContactsCount() method? Commented Sep 24, 2013 at 4:21
  • Post the code of getContactsCount Commented Sep 24, 2013 at 4:28
  • 1
    You are closing cursor and then calling cursor.getCount, do int count = cursor.getCount(), cursor.Close(); return count Commented Sep 24, 2013 at 4:42

1 Answer 1

1

In your getContactsCount() you close the cursor and then you try to access it. Store the count in a local variable and then close the cursor.

Sign up to request clarification or add additional context in comments.

5 Comments

correct ..can you please tell me how to update value in android data base
can you do in my code it is difficult to understand other code for beginner
I don't know from where i will start ..As i insert "naveen" on key 1 if i want to update it "naveen sharma" then what to do..
Seems like your table name is 'name' and the column you want to change is named 'name' as well. Hence, the following should work: ContentValues newValues = new ContentValues(); newValues.put("name","naveen sharma"); db.update("name", newValues, "id=1", null);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.