我在Python中对一些SQLite数据库更改进行了原型化,并使用executemany()
快速批量更新大约24,000行:
cursor.executemany("UPDATE k_ele SET priority = ?, freq = ? WHERE k_ele.id = ?", valPriorities)
当我将Python代码移植到我的Android应用程序SQLiteOpenHelper.OnUpgrade()
中时,每次需要更新行时,我都会调用db.update:
ContentValues updateFields = new ContentValues();
while(keCursor.moveToNext())
{
updateFields.clear();
// ...Calculate the values for 'id', 'priority' and 'freq'...
updateFields.put("priority", priority);
updateFields.put("freq", freq);
// Update the database with the calculated information
db.update("k_ele", updateFields, "id = " + id.toString(), null);
}
我已经确认我的移植代码确实升级了数据库(OnUpgrade()
在一个单独的线程中运行,因此我可以在主线程上显示“Please Wait”DialogFragment),但它需要大约30秒升级脚本运行时,我收到了很多以下类型的控制台消息:
08-27 07:34:30.692 21123-21179/com.appname W/SQLiteConnectionPool: The connection pool for database '/data/data/com.appname/databases/AppDict.db' has been unable to grant a connection to thread 3602 (ModernAsyncTask #5) with flags 0x1 for 30.004002 seconds.
Connections: 1 active, 0 idle, 0 available.
Requests in progress:
executeForChangedRowCount started 65ms ago - running, sql="CREATE INDEX k_ele_freq ON k_ele (freq);"
08-27 07:35:00.612 21123-21175/com.appname W/SQLiteConnectionPool: The connection pool for database '/data/data/com.appname/databases/AppDict.db' has been unable to grant a connection to thread 3598 (ModernAsyncTask #1) with flags 0x1 for 60.003002 seconds.
Connections: 0 active, 1 idle, 0 available.
08-27 07:35:00.682 21123-21176/com.appname W/SQLiteConnectionPool: The connection pool for database '/data/data/com.appname/databases/AppDict.db' has been unable to grant a connection to thread 3599 (ModernAsyncTask #2) with flags 0x1 for 60.001003 seconds.
Connections: 1 active, 0 idle, 0 available.
Requests in progress:
prepare started 0ms ago - running, sql="UPDATE k_ele SET priority=?,freq=? WHERE id = 30227"
08-27 07:35:00.702 21123-21178/com.appname W/SQLiteConnectionPool: The connection pool for database '/data/data/com.appname/databases/AppDict.db' has been unable to grant a connection to thread 3601 (ModernAsyncTask #4) with flags 0x1 for 60.008003 seconds.
Connections: 1 active, 0 idle, 0 available.
Requests in progress:
prepare started 0ms ago - running, sql="UPDATE k_ele SET priority=?,freq=? WHERE id = 30239"
08-27 07:35:00.702 21123-21179/com.appname W/SQLiteConnectionPool: The connection pool for database '/data/data/com.appname/databases/AppDict.db' has been unable to grant a connection to thread 3602 (ModernAsyncTask #5) with flags 0x1 for 60.009003 seconds.
Connections: 1 active, 0 idle, 0 available.
Requests in progress:
executeForChangedRowCount started 0ms ago - running, sql="UPDATE k_ele SET priority=?,freq=? WHERE id = 30239"
08-27 07:35:30.612 21123-21175/com.appname W/SQLiteConnectionPool: The connection pool for database '/data/data/com.appname/databases/AppDict.db' has been unable to grant a connection to thread 3598 (ModernAsyncTask #1) with flags 0x1 for 90.005005 seconds.
Connections: 0 active, 1 idle, 0 available.
有没有更好的方法来转换executemany()
?我担心我会蛮力破解解决方案,并且有更有效的方法来循环并更新所有这些成千上万的记录。