通过ADODB/SQLiteODBC修改SQLite日志模式时Excel/VBA挂起问题咨询
VBA操作SQLite数据库锁等待过长问题复现
如下代码会在Temp文件夹中新建空白SQLite数据库,创建表并填充数据后暂停执行。此时在DB Browser for SQLite中交互打开该数据库,修改某一值但不提交更改,恢复代码执行后,Excel/VBA会挂起约1.5分钟,才在尝试通过ADODB/SQLiteODBC修改日志模式时抛出"database is locked"错误。
需要说明的是,本代码为专门复现问题的代码,报错触发原因已知,核心问题为报错前需要等待的时间过长。
Private Sub ConnectSQLiteAdoCommandSource() Dim Driver As String Driver = "SQLite3 ODBC Driver" Dim Database As String Database = Environ("Temp") & "\" & CStr(Format(Now, "yyyy-mm-dd_hh-mm-ss.")) _ & CStr((Timer * 10000) Mod 10000) & CStr(Round(Rnd * 10000, 0)) & ".db" Debug.Print Database Dim Options As String Options = "JournalMode=DELETE;SyncPragma=NORMAL;FKSupport=True;" Dim AdoConnStr As String AdoConnStr = "Driver=" & Driver & ";" & "Database=" & Database & ";" & Options Dim SQLQuery As String Dim RecordsAffected As Long Dim AdoCommand As ADODB.Command Set AdoCommand = New ADODB.Command With AdoCommand .CommandType = adCmdText .ActiveConnection = AdoConnStr .ActiveConnection.CursorLocation = adUseClient End With '''' ===== Create Functions table ===== '''' SQLQuery = Join(Array( _ "CREATE TABLE functions(", _ " name TEXT COLLATE NOCASE NOT NULL,", _ " builtin INTEGER NOT NULL,", _ " type TEXT COLLATE NOCASE NOT NULL,", _ " enc TEXT COLLATE NOCASE NOT NULL,", _ " narg INTEGER NOT NULL,", _ " flags INTEGER NOT NULL", _ ")" _ ), vbLf) With AdoCommand .CommandText = SQLQuery .Execute RecordsAffected, Options:=adExecuteNoRecords End With '''' ===== Insert rows into Functions table ===== '''' SQLQuery = Join(Array( _ "INSERT INTO functions", _ "SELECT * FROM pragma_function_list" _ ), vbLf) With AdoCommand .CommandText = SQLQuery .Execute RecordsAffected, Options:=adExecuteNoRecords End With '@Ignore StopKeyword Stop '''' Lock Db. For example, open in GUI admin tool and start a transaction '''' ===== Try changing journal mode ===== '''' On Error Resume Next With AdoCommand .CommandText = "PRAGMA journal_mode = 'WAL'" .Execute RecordsAffected, Options:=adExecuteNoRecords End With If Err.Number <> 0 Then Debug.Print "Error: #" & CStr(Err.Number) & ". " & vbNewLine & _ "Error description: " & Err.Description End If On Error GoTo 0 AdoCommand.ActiveConnection.Close End Sub
内容的提问来源于stack exchange,提问作者PChemGuy
相关产品推荐
相关产品推荐

