仕事でSQLiteをよく使うようになり、使いまわし出来るモジュールを作りました。
SQLiteForExcel-0.9.zipをこちらからダウンロードして解凍し、
同じディレクトリにデータベース[data.db]を作成します。
引数SQLStrにSQL文を入れ実行します。
以下備忘録のコードです。
'引数 SQLStr に任意のSQL文を入れてください。
Public Sub SQLiteExecution(SQLStr As String)
#If Win64 Then
Dim myDbHandle As LongPtr
Dim myStmtHandle As LongPtr
#Else
Dim myDbHandle As Long
Dim myStmtHandle As Long
#End If
Dim RetVal As Long
Dim recordsAffected As Long
Dim stepMsg As String
Dim TestFile as String
TestFile = ThisWorkbook.Path & "\data.db"
Debug.Print "----- SQL_Start -----"
RetVal = SQLite3Open(TestFile, myDbHandle)
Debug.Print "SQLite3Open returned " & RetVal
RetVal = SQLite3PrepareV2(myDbHandle, SQLStr, myStmtHandle)
Debug.Print "SQLite3PrepareV2 returned " & RetVal
RetVal = SQLite3Step(myStmtHandle)
If RetVal = SQLITE_DONE Then
Debug.Print "SQLite3Step Done"
Else
Debug.Print "SQLite3Step returned " & RetVal
End If
RetVal = SQLite3Finalize(myStmtHandle)
Debug.Print "SQLite3Finalize returned " & RetVal
RetVal = SQLite3Close(myDbHandle)
Debug.Print "----- SQL_End -----"
Call SQLite3Free
End Sub