在下面的代码中,连接到sdcard上的数据库,数据库打开但是在logcat中显示错误。
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; } }
和logcat错误是:
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)
谢谢你的帮助。
答案 0 :(得分:1)
我创建了这个bug。在while (cursor.moveToFirst());
中更改为while (cursor.moveToNext());
thanks
答案 1 :(得分:0)
可能是在关闭之前完成了Cursor的错误,您需要在执行所需的任务后通过代码中的mCursor.close()
关闭光标。