0
votes

In below code, connect to database on sdcard, database open but in logcat show error.

package com.avizhegroup.islamatlas.SQLiteMethods;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.avizhegroup.islamatlas.model.Point;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "Data.db";

    private static final String TABLE_POINTDATA = "PointData";
    private static final String TABLE_POINTDATA_PRIMARYCATEGORYID = "PrimaryCategoryId";
    private static final String TABLE_POINTDATA_SECONDARYCATEGORYID = "SecondaryCategoryId";
    private static final String TABLE_POINTDATA_POINTID = "PointId";
    private static final String TABLE_POINTDATA_POINTNAME = "PointName";
    private static final String TABLE_POINTDATA_POINTSUMMARY = "PointSummary";
    private static final String TABLE_POINTDATA_POINTLAT = "PointLat";
    private static final String TABLE_POINTDATA_POINTLNG = "PointLng";
    private static final String TABLE_POINTDATA_POINTMARKERKIND = "PointMarkerKind";
    private static final String TABLE_POINTDATA_TAG = "POINT DATA TAG";

    private SQLiteDatabase db;

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    }

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

    // Getting All Points
    public List getAllPoints() {
        List pointsList = new ArrayList();

        String selectQuery = "SELECT * FROM " + TABLE_POINTDATA;
        File dbfile = new File(Environment.getExternalStorageDirectory()
                .getPath() + File.separator + "Data.db");
        db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);

        if (db.isOpen()) {
            Log.d("ISLAM ATLAS", "database is Open");
        }

        Cursor cursor = db.rawQuery(selectQuery, null);
        try {
            if (cursor.moveToFirst()) {
                do {
                    Point point = new Point();
                    point.setPrimaryCategoryId(Integer.parseInt(cursor
                            .getString(0)));
                    point.setSecondaryCategoryId(Integer.parseInt(cursor
                            .getString(1)));
                    point.setId(Integer.parseInt(cursor.getString(2)));
                    point.setName(cursor.getString(3));
                    point.setSummary(cursor.getString(4));
                    point.setLatitude(Double.parseDouble(cursor.getString(5)));
                    point.setLongtitude(Double.parseDouble(cursor.getString(6)));
                    point.setType(cursor.getString(7));

                    pointsList.add(point);

                } while (cursor.moveToFirst());
            }
        } finally {
            cursor.deactivate();
            cursor.close();
            db.close();
        }
        return pointsList;
    }
}

and logcat error is :

10-20 10:31:33.069: E/Database(460): close() was never explicitly called on database '/mnt/sdcard/Data.db' 
10-20 10:31:33.069: E/Database(460): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
10-20 10:31:33.069: E/Database(460):    at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1810)
10-20 10:31:33.069: E/Database(460):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
10-20 10:31:33.069: E/Database(460):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
10-20 10:31:33.069: E/Database(460):    at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
10-20 10:31:33.069: E/Database(460):    at com.avizhegroup.islamatlas.SQLiteMethods.DatabaseHandler.getAllPoints(DatabaseHandler.java:51)
10-20 10:31:33.069: E/Database(460):    at com.avizhegroup.islamatlas.MainActivity.onCreate(MainActivity.java:40)
10-20 10:31:33.069: E/Database(460):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

thanks for helping me.

2
After try you must add `catch'EduardoSaverin
@Johnsmith I do this, but that error happened. should write code in onCreate() method when use database on sdcard ?Hajitsu
onCreate() is Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen.EduardoSaverin
Is your database is used by another applicationEduardoSaverin
@Johnsmith No, in AVD only my app installed.Hajitsu

2 Answers

1
votes

I founded the bug. in while (cursor.moveToFirst()); change to while (cursor.moveToNext()); thanks

0
votes

probably you are getting an error of Cursor finalized before the close, you need to close the cursor by mCursor.close() in your code after performing the required task.