我用 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()
在单独的线程中运行,因此我可以在主线程上显示“请稍候”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()
?我担心我会暴力破解解决方案,并且有一种更有效的方法来循环和更新所有这些数以万计的记录。