Monday, 22 May 2017

android - Cursor for database on sdcard not closing or deactive

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.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...