android – SQLiteDatabase错误,无用的日志
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android – SQLiteDatabase错误,无用的日志,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2165字,纯文字阅读大概需要4分钟。
内容图文
![android – SQLiteDatabase错误,无用的日志](/upload/InfoBanner/zyjiaocheng/888/615346e126434cf38f73e81aeb252e83.jpg)
我发布了我的应用程序的更新,我收到了很多用户的错误,我无法重新创建它或指出问题所在.
我得到的两个错误:
java.lang.IllegalStateException:尝试重新打开已关闭的对象:SQLiteDatabase
java.lang.IllegalStateException:数据库未打开
错误发生在以下代码中的某处:
public DBUserC getUser(int _id){
synchronized (DBAdapter.LOCK){
M.openDB(context);
Cursor cursor = M.database.query(DBUserHelper.TABLE_USERS,
VUser.allColumns, DBUserHelper.COLUMN_ID + " = '"+_id+"'", null, null, null, null);
DBUserC user;
if (cursor.moveToFirst()){
user = cursorToInfo(cursor);
} else{
user = newUser();
}
cursor.close();
M.closeDB();
return user;
}
}
在这个版本中,有一个数据库升级,我通过db.execSql执行三个查询.它不在一个单独的线程中.
在每次调用数据库时(onUpgrade除外),我同步,然后打开它,运行我的代码,然后关闭它.在升级之前我没有遇到任何问题,但无法找到问题.
任何帮助将不胜感激.
编辑:要打开我的数据库,我做:
if (helper==null)
helper = new DBAdapter(context);
if (database==null){
database = helper.getWritableDatabase();
} else if (!database.isOpen())
database = helper.getWritableDatabase();
并关闭:
helper.close();
helper = null;
database = null;
获取信息的示例方法:
public DBUserC getUser(int _id){
synchronized (DBAdapter.LOCK){
openDB(context);//this is the open code above
Cursor cursor = M.database.query(DBUserHelper.TABLE_USERS,
VUser.allColumns, DBUserHelper.COLUMN_ID + " = '"+_id+"'", null, null, null, null);
DBUserC user;
if (cursor.moveToFirst()){
user = cursorToInfo(cursor);//does not contain DB operations
} else{
user = newUser(); ////does not contain Database operations
}
cursor.close();
closeDB();//This is the close code above
return user;
}
}
AHost.onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
M.initializeDB(context); //synchronized call to the openDB code I posted above
M.openDataDB(context); //opens a different database by a different file name. This DB is not an issue
//irrelevant ui setup
int _id = pref.getInt(P.eLastUser, VUser.ID_NEW);//row id of user
M.requeryUser();//synchronized, access database
M.switchUser(_id);//synchronized, access database
}
解决方法:
我在我的一个应用程序中遇到了同样的问题,我如何通过删除cursor.close()解决了这个问题.希望它会有所帮助:)
内容总结
以上是互联网集市为您收集整理的android – SQLiteDatabase错误,无用的日志全部内容,希望文章能够帮你解决android – SQLiteDatabase错误,无用的日志所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。