Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SQLite数据库:SQLiteDiskIOException:磁盘I/O错误(代码1802)_Android_Database_Sqlite - Fatal编程技术网

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

Android SQLite数据库:SQLiteDiskIOException:磁盘I/O错误(代码1802),android,database,sqlite,Android,Database,Sqlite,我有一个利用SQLite数据库的应用程序 我在应用程序启动时创建了数据库,调用了RSSDatabase的构造函数,效果很好。单击按钮后,我从RSS提要获取数据并在异步任务中插入数据,但调用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

我有一个利用SQLite数据库的应用程序

我在应用程序启动时创建了数据库,调用了RSSDatabase的构造函数,效果很好。单击按钮后,我从RSS提要获取数据并在
异步任务中插入数据
,但调用
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);
    }
}
有人能解释一下这个问题吗


提前谢谢

当应用程序启动时,调用RSSDatabase的构造函数,UI线程将锁定数据库

db = dbHelper.getWritableDatabase(); // In the constructor

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

检查内容中是否存在任何空值values@RajeshCP我相信这不是问题所在,因为我在表定义中没有“NOTNULL”,这是本机级别的
SQLITE\u IOERR\u FSTAT
。你的数据库有多大?有没有并发读/写/删除访问?@laalto数据库几乎是空的。我相信数据库不会被多个线程同时访问。@laalto但是,由于数据库插入是在异步任务中执行的,因此实际上可能存在一些并发问题。我在查,谢谢!这正是我在将代码移出UI线程后发现的。等等,什么?为什么它不抛出“数据库被锁定(代码5)”而不是“磁盘I/O错误(代码10)”?@Guanlun您是如何解决这个数据库锁定问题的?因为这个我被封锁了。