Android的插入由java.lang.IllegalStateException造成数据的SQLite错误:数据库打不开 [英] Android insert the data SQLite error Caused by java.lang.IllegalStateException: database not open

查看:574
本文介绍了Android的插入由java.lang.IllegalStateException造成数据的SQLite错误:数据库打不开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从服务器获取数据,我插入数据到我自己的内容提供商,但它总是错误。这是日志

When I get the data from the server, I insert the data into the my own content provider, but it always error. This is the log

 Caused by: java.lang.IllegalStateException: database not open
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1585)
at com.weiboa.data.StatusProvider.insert(StatusProvider.java:107
    at android.content.ContentProvider$Transport.insert(ContentProvider.java:210)
    at android.content.ContentResolver.insert(ContentResolver.java:606)
    at com.weiboa.util.WeiboUserUtil.insertTweet(WeiboUserUtil.java:121)

这是我插入code我的的ContentProvider

SQLiteDatabase db = dbHelper.getWritableDatabase();
try{
    long id = db.insertWithOnConflict(TABLE, null, values,SQLiteDatabase.CONFLICT_REPLACE);
    if(id == -1){
        throw new RuntimeException(String.format("%s : Failed to insert [%s] to [%s] for unknow reason,", TAG, values, uri));
    }else{
        return ContentUris.withAppendedId(uri, id);
    }
 }finally{
    db.close();
 }

我发现在Android源$ C ​​$ c中的错误信息,它将检查SQLiteDatabase.isOpen(),并在功能getWritableDatabase,它总是检查数据库是开放的?

And i find the error message in the android source code, it will check the SQLiteDatabase.isOpen(), and in the function getWritableDatabase, it always checked the database is open?

if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
        return mDatabase;  // The database is already open for business
    }

所以我不知道真正的原因造成这个错误,为什么不打开数据库。

So I don't know the real reason to caused this error why the database is not opened.

推荐答案

我有完全相同的问题。

由于@Luksprog第一条评论中提及只是删除

As @Luksprog mentioned in the first comment just remove the

db.close();

在一般情况下,在内容提供商,你从来没有明确地关闭数据库。助手会为你。

In general, in a content provider, you never have to explicitly close the Database. The helper does it for you.

这篇关于Android的插入由java.lang.IllegalStateException造成数据的SQLite错误:数据库打不开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆