Android SQLite数据库:SQLiteDiskIOException:磁盘I / O错误(代码1802)

前端之家收集整理的这篇文章主要介绍了Android SQLite数据库:SQLiteDiskIOException:磁盘I / O错误(代码1802)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用sqlite数据库的应用程序.

我在应用程序启动时创建了数据库,调用RSSDatabase的构造函数,工作正常.单击一个按钮后,我从RSS提要中获取数据并在AsyncTask中插入数据,但是当我调用db.insert(“posts”,“”,values)时会导致错误,这是logcat的错误消息:

02-04 21:43:22.050  19154-19357/com.example.aolreader E/sqliteLog﹕ (1802) os_unix.c:30026: (2) stat(/data/data/com.example.aolreader/databases/RSS_db) -
02-04 21:43:22.050  19154-19357/com.example.aolreader E/sqliteLog﹕ (1802) statement aborts at 13: [INSERT INTO posts(author,title,date,description,link) VALUES (?,?,?)]
02-04 21:43:22.050  19154-19357/com.example.aolreader E/sqliteDatabase﹕ Error inserting author=AP title=UK admits ‘limited’ role in Operation Blue Star  date=Tue,4 Feb 2014 19:03:27 +0530 description=
    The British government had recently ordered an urgent investigation into possible U.K. involvement in the raid.
    link=http://www.thehindu.com/news/international/world/uk-admits-limited-role-in-operation-blue-star/article5653316.ece
    android.database.sqlite.sqliteDiskIOException: disk I/O error (code 1802)
            at android.database.sqlite.sqliteConnection.nativeExecuteForLastInsertedRowId(Native Method)
            at android.database.sqlite.sqliteConnection.executeForLastInsertedRowId(sqliteConnection.java:782)
            at android.database.sqlite.sqliteSession.executeForLastInsertedRowId(sqliteSession.java:788)
            at android.database.sqlite.sqliteStatement.executeInsert(sqliteStatement.java:86)
            at android.database.sqlite.sqliteDatabase.insertWithOnConflict(sqliteDatabase.java:1469)
            at android.database.sqlite.sqliteDatabase.insert(sqliteDatabase.java:1339)
            at com.example.aolreader.RSSDatabase.insertPost(RSSDatabase.java:44)
            at com.example.aolreader.XMLRSSActivity.parseNews(XMLRSSActivity.java:386)
            at com.example.aolreader.XMLRSSActivity.access$000(XMLRSSActivity.java:33)
            at com.example.aolreader.XMLRSSActivity$ParseTask.doInBackground(XMLRSSActivity.java:171)
            at com.example.aolreader.XMLRSSActivity$ParseTask.doInBackground(XMLRSSActivity.java:134)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

搜索了这个错误,发现它可能是由同一时间访问数据库的不同线程引起的,但我查看了我的代码,发现没有多线程有竞争条件的情况.

这是我的数据库助手类:

class RSSDatabaseHelper extends sqliteOpenHelper {

    private static final String createPostTablesql = "create table posts (" +
            "_id integer primary key autoincrement," +
            "title varchar," +
            "author varchar," +
            "link varchar," +
            "description varchar," +
            "date varchar)";

    private Context context;

    public RSSDatabaseHelper(Context c,String name,sqliteDatabase.CursorFactory factory,int version) {
        super(c,name,factory,version);
        context = c;
    }

    @Override
    public void onCreate(sqliteDatabase db) {
        db.execsql(createPostTablesql);
    }

    @Override
    public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
        db.execsql("drop table if exists posts");
        onCreate(db);
    }
}

我的数据库类:

public class RSSDatabase {
    private static RSSDatabase instance;

    private sqliteDatabase db;
    private RSSDatabaseHelper dbHelper;

    private RSSDatabase(Context c) {
        dbHelper = new RSSDatabaseHelper(c,"RSS_db",null,1);
        db = dbHelper.getWritableDatabase();
    }

    public static synchronized RSSDatabase getDatabaseInstance(Context c) {
        if (instance == null) {
            instance = new RSSDatabase(c);
        }
        return instance;
    }

    public void insertPost(Post post) {
        ContentValues values = new ContentValues();
        values.put("title",post.title);
        values.put("author",post.author);
        values.put("link",post.link);
        values.put("description",post.description);
        values.put("date",post.date);
        db.insert("posts","",values);
    }
}

谁能解释一下这个问题呢?

提前致谢!

解决方法@H_502_22@
当应用程序启动时,调用RSSDatabase的构造函数,UI Thread会锁定您的数据库
db = dbHelper.getWritableDatabase(); // In the constructor

现在从Async任务(不同的线程),您正在尝试在由UI线程锁定的数据库中插入数据.这将引发异常,因为第一次写入对db有锁定.

原文链接:https://www.f2er.com/android/316310.html

猜你在找的Android相关文章