删除大型sqlite表

时间:2017-07-27 04:44:32

标签: android sqlite android-sqlite greendao greendao-generator

我陷入了独特的境地。在我的应用程序中,我有本地数据库。不知何故,其中一个表填满了垃圾数据,数据库变得非常庞大。当我知道时,大小超过1 GB。所以我继续获得数据库锁定异常,因为读取该表上的垃圾行非常慢。调试之后,我弄清楚它是哪个表,现在我想删除它(删除整个表或删除所有行)但每当我对该表执行任何操作时,数据库都会被锁定并发生ANR。所以我不确定如何摆脱那张桌子。我无法卸载应用。

请注意,我在我的Android应用中使用了GreenDao。

我试过了以下事情

1) DROP TABLE mytable;
2) DELETE FROM mytable;
3) DaoSession.getMyDao().deleteAll(); // this is a greedao method but it internally performs 2nd query I mentioned I think

在所有情况下。它只是生成ANR.and数据库被锁定。

编辑:我也在单独的线程中尝试过它,它只是避免了ANR,但没有删除表,数据库仍然被锁定。

锁定之后,每当其他线程试图写入db时,我就会得到这个,这很明显。

E/SQLiteDatabase: Failed to open database '/data/user/0/myapp/databases/mydb.db'.
 android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5): , while compiling: PRAGMA journal_mode
 #################################################################
 Error Code : 5 (SQLITE_BUSY)
 Caused By : The database file is locked.
 (database is locked (code 5): , while compiling: PRAGMA journal_mode)
 #################################################################
 at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
 at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1000)
 at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:704)
 at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:385)
 at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:359)
 at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:248)
 at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:199)
 at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:514)
 at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206)
 at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
 at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:934)
 at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:895)
 at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:708)
 at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:646)
 at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:283)
 at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
 at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
 at com.intouchapp.database.IntouchDb.getNewReadableDaoSesssion(IntouchDb.java:80)
 at com.intouchapp.models.ActivityLogsDb.getCursorOfAllResults(ActivityLogsDb.java:298)
 at com.intouchapp.services.ActivityLogsDbInsertionService.onHandleIntent(ActivityLogsDbInsertionService.java:73)
 at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:148)
 at android.os.HandlerThread.run(HandlerThread.java:61)

我也是这样,

The connection pool for database '+data+user+0+myapp+databases+mydb' has been unable to grant a connection to thread 2262 (SyncAdapterThread-1) with flags 0x2 for 12.0060005 seconds.
Connections: 1 active, 0 idle, 0 available.

Requests in progress:
executeForChangedRowCount started 12645ms ago - running, sql="DELETE FROM 'mytable'"

这清楚地表明它无法执行"DELETE FROM 'mytable'"此特定查询因此锁定。

1 个答案:

答案 0 :(得分:0)

SQLite在用于编写操作时锁定数据库。 您应该注意以下几点: -

  • DataBase操作应该在另一个不在UI线程中的线程中使用。
  • 创建SQLiteOpenHelper类的单个实例
  • 完成任务后关闭所有数据库实例
  • 始终使用endTransaction()
  • 关闭finally块中的所有数据库实例