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()
ますか? 私は解決策を強引に実行しているのではないかと心配しています。また、これらの数万のレコードすべてをループして更新するより効率的な方法があるのではないかと心配しています。