以下是我的代码,无法关闭数据库正确
public class CommentsDataSource {
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_COMMENT };
public CommentsDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
if (database != null) {
database.close();
}
dbHelper.close();
}
public String getComment_1() {
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Comment comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return comments.get(0).getComment();
}
private Comment cursorToComment(Cursor cursor) {
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
return comment;
}
public class MyService extends BackgroundService implements LocationListener {
@Override
public void onCreate() {
context = getApplicationContext();
datasource = new CommentsDataSource(context);
gps = new GPSTracker(context);
datasource.open();
mHelloTo = datasource.getComment_1();
datasource.close();
}
}
在上面的代码用于检索后台服务从内部数据库中的一些数据。它工作正常,但有时它会给出以下错误,即使我正确关闭游标数据库和dbhelper。
01-08 14:31:58.691: E/SQLiteDatabase(13854): close() was never explicitly called on database '/data/data/org.apache.cordova.example/databases/commments.db'
不知道这个的
MySQLiteHelper
相关部分,但你可以尝试移动内部getComment_1(以下简称“cursorToComment”代码)(又名破坏“cursorToComment”方法) – hovanessyan