gpt4 book ai didi

Android SQLite 数据库 : SQLiteDiskIOException: disk I/O error (code 1802)

转载 作者:IT王子 更新时间:2023-10-29 06:23:28 32 4
gpt4 key购买 nike

我有一个使用 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);
}
}

谁能阐明这个问题?

提前致谢!

最佳答案

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

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

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

关于Android SQLite 数据库 : SQLiteDiskIOException: disk I/O error (code 1802),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21554277/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com