Android 无法正确关闭数据库
下面是我的代码Android 无法正确关闭数据库,android,sqlite,Android,Sqlite,下面是我的代码 public class CommentsDataSource { private SQLiteDatabase database; private MySQLiteHelper dbHelper; private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_COMMENT };
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();
}
}
尝试更改此选项:
public String getComment_1() {
open(); // open database
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();
close(); // close database
return comments.get(0).getComment();
}
公共字符串getComment_1(){
open();//打开数据库
列表注释=新建ArrayList();
Cursor Cursor=database.query(MySQLiteHelper.TABLE_注释,
所有列,null,null,null,null,null,null);
cursor.moveToFirst();
而(!cursor.isAfterLast()){
注释注释=游标注释(游标);
注释。添加(注释);
cursor.moveToNext();
}
//确保关闭光标
cursor.close();
close();//关闭数据库
返回注释。get(0.getComment();
}
还有什么
dbHelper.close()代码>是什么?您正在关闭与调用数据库的db的连接。close()
那么为什么需要它呢?您正确地调用了方法,但是请将打开()
和关闭()
方法更改为以下内容:
public void open() throws SQLException {
dbHelper= new MySQLiteHelper (ctx);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}
但是,与每次执行读/写操作时打开和关闭数据库不同,我建议您在
应用程序中打开数据库一次。当您的应用程序退出或被Android操作系统关闭时,关闭将自动完成。由于SQLite数据的完整性,您不必担心数据丢失。这只发生在JellyBean之前的平台上。JellyBean和更高版本已删除此错误消息
即使它是以错误日志级别记录的,并且包含异常stacktrace,但它不是引发的异常SQLiteDatabase
constructor只将调用方的stacktrace存储在成员变量exception中,如果数据库仍然打开,则在finalizer中记录stacktrace
您可以查看stacktrace以查看未关闭数据库的打开位置
要获得有关代码的特定帮助,请在问题中包含MySQLiteHelper
的相关部分对此不确定,但您能否尝试将“cursorToComment”代码移动到getComment_1()中(也称为销毁“cursorToComment”方法)
public void open() throws SQLException {
dbHelper= new MySQLiteHelper (ctx);
database = dbHelper.getWritableDatabase();
return this;
}
public void close() {
dbHelper.close();
}