android – 使用SQLite数据库时的几个异常

android – 使用SQLite数据库时的几个异常,第1张

概述这段代码: public void foo() { new Thread() { @Override public void run() { ShaufelDatabaseHelper dbHelper = ShaufelDatabaseHelper.getInstance(parentActivity); S 这段代码:
public voID foo() {    new Thread() {        @OverrIDe        public voID run() {            ShaufelDatabaseHelper dbHelper = ShaufelDatabaseHelper.getInstance(parentActivity);            sqliteDatabase connection = null;            Cursor queryData = null;            final String[] values = new String[8];            try{                connection = dbHelper.getReadableDatabase();                queryData = connection.rawquery(         // line 132                    "SELECT name,firstname,section,row,gravenr,type,workorder,comment "+                    "FROM orders,graves "+                    "WHERE orders.ID=? AND graves.ID=orders.ID",new String[] {orderID});                queryData.movetoFirst(); // line 138                // ...        } catch(Throwable t) {            // ...        }    }.start();}

给我这些例外:

10-29 15:19:55.103: E/AndroIDRuntime(23102): FATAL EXCEPTION: Thread-138710-29 15:19:55.103: E/AndroIDRuntime(23102): java.lang.IllegalMonitorStateException10-29 15:19:55.103: E/AndroIDRuntime(23102):    at java.util.concurrent.locks.reentrantlock$Sync.tryRelease(reentrantlock.java:126)10-29 15:19:55.103: E/AndroIDRuntime(23102):    at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1239)10-29 15:19:55.103: E/AndroIDRuntime(23102):    at java.util.concurrent.locks.reentrantlock.unlock(reentrantlock.java:431)10-29 15:19:55.103: E/AndroIDRuntime(23102):    at androID.database.sqlite.sqliteDatabase.unlock(sqliteDatabase.java:487)10-29 15:19:55.103: E/AndroIDRuntime(23102):    at androID.database.sqlite.sqliteDirectCursorDriver.query(sqliteDirectCursorDriver.java:62)10-29 15:19:55.103: E/AndroIDRuntime(23102):    at androID.database.sqlite.sqliteDatabase.rawqueryWithFactory(sqliteDatabase.java:1564)10-29 15:19:55.103: E/AndroIDRuntime(23102):    at androID.database.sqlite.sqliteDatabase.rawquery(sqliteDatabase.java:1538)10-29 15:19:55.103: E/AndroIDRuntime(23102):    at de.l_one.app.shaufel.OrderDetailVIEw.run(OrderDetailVIEw.java:132)10-29 15:21:07.953: E/sqlitequery(23133): exception: library routine called out of sequence; query: SELECT name,comment FROM orders,graves WHERE orders.ID=? AND graves.ID=orders.ID10-29 15:21:07.953: E/sql order detail(23133): androID.database.sqlite.sqliteMisuseException: library routine called out of sequence10-29 15:21:07.953: E/sql order detail(23133): androID.database.sqlite.sqliteMisuseException: library routine called out of sequence10-29 15:21:07.953: E/sql order detail(23133):  at androID.database.sqlite.sqlitequery.nativeFillWindow(Native Method)10-29 15:21:07.953: E/sql order detail(23133):  at androID.database.sqlite.sqlitequery.fillWindow(sqlitequery.java:86)10-29 15:21:07.953: E/sql order detail(23133):  at androID.database.sqlite.sqliteCursor.fillWindow(sqliteCursor.java:164)10-29 15:21:07.953: E/sql order detail(23133):  at androID.database.sqlite.sqliteCursor.getCount(sqliteCursor.java:156)10-29 15:21:07.953: E/sql order detail(23133):  at androID.database.AbstractCursor.movetoposition(AbstractCursor.java:161)10-29 15:21:07.953: E/sql order detail(23133):  at androID.database.AbstractCursor.movetoFirst(AbstractCursor.java:201)10-29 15:21:07.953: E/sql order detail(23133):  at de.l_one.app.shaufel.OrderDetailVIEw.run(OrderDetailVIEw.java:138)10-29 15:22:52.983: E/AndroIDRuntime(23133): FATAL EXCEPTION: Thread-141110-29 15:22:52.983: E/AndroIDRuntime(23133): java.lang.IllegalStateException: database /data/data/de.l_one.app.shaufel/databases/poIDatabase (conn# 0) already closed10-29 15:22:52.983: E/AndroIDRuntime(23133):    at androID.database.sqlite.sqliteDatabase.verifyDbIsOpen(sqliteDatabase.java:2082)10-29 15:22:52.983: E/AndroIDRuntime(23133):    at androID.database.sqlite.sqliteCompiledsql.<init>(sqliteCompiledsql.java:58)10-29 15:22:52.983: E/AndroIDRuntime(23133):    at androID.database.sqlite.sqliteProgram.compilesql(sqliteProgram.java:143)10-29 15:22:52.983: E/AndroIDRuntime(23133):    at androID.database.sqlite.sqliteProgram.compileAndbindAllArgs(sqliteProgram.java:361)10-29 15:22:52.983: E/AndroIDRuntime(23133):    at androID.database.sqlite.sqliteProgram.<init>(sqliteProgram.java:127)10-29 15:22:52.983: E/AndroIDRuntime(23133):    at androID.database.sqlite.sqliteProgram.<init>(sqliteProgram.java:94)10-29 15:22:52.983: E/AndroIDRuntime(23133):    at androID.database.sqlite.sqlitequery.<init>(sqlitequery.java:53)10-29 15:22:52.983: E/AndroIDRuntime(23133):    at androID.database.sqlite.sqliteDirectCursorDriver.query(sqliteDirectCursorDriver.java:47)10-29 15:22:52.983: E/AndroIDRuntime(23133):    at androID.database.sqlite.sqliteDatabase.rawqueryWithFactory(sqliteDatabase.java:1564)10-29 15:22:52.983: E/AndroIDRuntime(23133):    at androID.database.sqlite.sqliteDatabase.rawquery(sqliteDatabase.java:1538)10-29 15:22:52.983: E/AndroIDRuntime(23133):    at de.l_one.app.shaufel.OrderDetailVIEw.run(OrderDetailVIEw.java:132)

这些不会在一次运行中抛出,而是(似乎是)随机抛出.抛出Exception时没有模式,有时根本不会抛出Exception.
我在我的应用程序中使用相同的方法,它没有任何问题.我没有最小的线索来自IllegalStateException,因为数据库没有关闭,至少不是我. (根据SO的一些答案,这是正常的,在某些情况下甚至比手动关闭你的dbs更好).

代码注释:
ShaufelDatabaseHelper是一个扩展sqliteOpenHelper的Singleton.它用于一般工作的不同活动,这似乎是唯一的例外.

我刚刚在第二台设备上测试过,没有抛出任何异常,一切正常.
抛出Exceptions的设备是运行androID 4.0.3的lenovo thinkpad.
没有Exceptions运行的是运行AndroID 2.3.4的HTC Sensation Z710e.

有谁知道这会导致什么?

E:我不知道它是否有帮助但是在sqliteMisuseException之前记录了这个:

10-29 16:07:46.530: I/sqliteDatabaseCpp(23728): sqlite returned: error code = 21,msg = API called with NulL prepared statement,db=/data/data/de.l_one.app.shaufel/databases/poIDatabase10-29 16:07:46.530: I/sqliteDatabaseCpp(23728): sqlite returned: error code = 21,msg = misuse at line 58929 of [8609a15dfa],msg = API call with invalID database connection pointer,msg = misuse at line 106340 of [8609a15dfa],msg = misuse at line 106271 of [8609a15dfa],db=/data/data/de.l_one.app.shaufel/databases/poIDatabase
解决方法 原因

如果有任何机会,另一个线程在你调用getReadableDatabase()和rawquery()之间或者在执行rawquery()期间关闭了数据库,你将得到这个异常.这是因为您的sqliteOpenHelper是一个单例(它应该是),这意味着如果某个其他线程关闭了数据库,则将关闭保存数据库连接的单个内部mDatabase成员.

我的建议:

为您的sqliteOpenHelper派生实现自己的close()方法.在您的实现中,在实际关闭之前检查已打开连接的引用计数(调用sqliteOpenHelper.close()).

private int openedConnections = 0;//...public synchronized sqliteDatabase getReadableDatabase() {    openedConnections++;    getReadableDatabase();}public synchronized voID close() {    openedConnections--;    if (openedConnections == 0) {        close();    }}

让我知道它是否有效! 总结

以上是内存溢出为你收集整理的android – 使用SQLite数据库时的几个异常全部内容,希望文章能够帮你解决android – 使用SQLite数据库时的几个异常所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1138944.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存