在下面的代码中,连接到 sdcard 上的数据库,数据库打开但在 logcat 中显示错误。
包 com.avizhegroup.islamatlas.SQLiteMethods; 导入java.io.File; 导入 java.util.ArrayList; 导入 java.util.List; 导入 com.avizhegroup.islamatlas.model.Point; 导入android.content.Context; 导入android.database.Cursor; 导入android.database.sqlite.SQLiteDatabase; 导入 android.database.sqlite.SQLiteOpenHelper; 导入android.os.Environment; 导入android.util.Log; 公共类 DatabaseHandler 扩展 SQLiteOpenHelper { 私有静态最终字符串 DATABASE_NAME = "Data.db"; 私有静态最终字符串 TABLE_POINTDATA = "PointData"; 私有静态最终字符串 TABLE_POINTDATA_PRIMARYCATEGORYID = "PrimaryCategoryId"; 私有静态最终字符串 TABLE_POINTDATA_SECONDARYCATEGORYID = "SecondaryCategoryId"; 私有静态最终字符串 TABLE_POINTDATA_POINTID = "PointId"; 私有静态最终字符串 TABLE_POINTDATA_POINTNAME = "PointName"; 私有静态最终字符串 TABLE_POINTDATA_POINTSUMMARY = "PointSummary"; 私有静态最终字符串 TABLE_POINTDATA_POINTLAT = "PointLat"; 私有静态最终字符串 TABLE_POINTDATA_POINTLNG = "PointLng"; 私有静态最终字符串 TABLE_POINTDATA_POINTMARKERKIND = "PointMarkerKind"; private static final String TABLE_POINTDATA_TAG = "POINT DATA TAG"; 私有 SQLiteDatabase 数据库; 公共数据库处理程序(上下文上下文){ 超级(上下文,DATABASE_NAME,空,1); } @覆盖 公共无效onCreate(SQLiteDatabase db){ } @覆盖 公共无效 onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ } // 获取所有分数 公共列表 getAllPoints() { 列出 pointsList = new ArrayList(); 字符串 selectQuery = "SELECT * FROM " + TABLE_POINTDATA; 文件 dbfile = 新文件(Environment.getExternalStorageDirectory() .getPath() + File.separator + "Data.db"); db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); 如果(db.isOpen()){ Log.d("伊斯兰地图集", "数据库已打开"); } 光标 cursor = db.rawQuery(selectQuery, null); 尝试 { if (cursor.moveToFirst()) { 做 { 点点 = 新点(); point.setPrimaryCategoryId(Integer.parseInt(光标 .getString(0))); point.setSecondaryCategoryId(Integer.parseInt(光标 .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); } 而 (cursor.moveToFirst()); } } 最后 { cursor.deactivate(); 光标.close(); db.close(); } 返回积分列表; } }
和 logcat 错误是:
10-20 10:31:33.069: E/Database(460): close() 从未在数据库'/mnt/sdcard/Data.db' 上显式调用 10-20 10:31:33.069: E/Database(460): android.database.sqlite.DatabaseObjectNotClosedException: 应用程序没有关闭在此处打开的游标或数据库对象 10-20 10:31:33.069: E/Database(460): 在 android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1810) 10-20 10:31:33.069: E/Database(460): 在 android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817) 10-20 10:31:33.069: E/Database(460): 在 android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851) 10-20 10:31:33.069: E/Database(460): 在 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): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
谢谢你帮助我。