2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[VBA]SQLite For Excel でSQL実行モジュール

Last updated at Posted at 2019-08-31

仕事で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
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?