是否有一个Android等价于Python的executemany()?

2024-10-01 07:51:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中创建了一些SQLite数据库更改的原型,并使用executemany()来快速更新大约24000行:

cursor.executemany("UPDATE k_ele SET priority = ?, freq = ? WHERE k_ele.id = ?", valPriorities)

当我将Python代码移植到我的Android应用程序的SQLiteOpenHelper.OnUpgrade()时,我最后调用了数据库更新每次我需要更新行时:

^{pr2}$

我已经确认,我移植的代码确实升级了数据库(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()?我担心的是,我会粗暴地强制执行解决方案,而且会有一种更有效的方法来遍历和更新所有这些成千上万的记录。在


Tags: thetocomfordbdataconnectiondatabase