Windows Dev. Site

SQLite for Excel

一つのファイルでデータベースを扱える手軽さから、さまざまなプラットホームで使われているSQLite。Excelから直接扱えることを知り、テストしてみました。

http://sqliteforexcel.codeplex.com/

このサイトからダウンロードしたファイルだけで、特に何かインストールすることなくDBファイルに読み書きできました。
Distributionフォルダにある、SQLiteForExcel_64.xlsmを使用。
sqlite3.dllはx64フォルダのものに変更します。

環境 : Excel 2013 / Windows 10
このファイルには、SQlite3とSQlite3DemoというVBAモジュールがすでに組み込まれており、SQlite3Demoを参考に以下のプログラムを作成しました。
ボタン1でDBの作成、データ書き込み、ボタン2でDB読み込みセルに表示をしています。

Sub ボタン1_Click()
    Dim InitReturn As Long
    Dim testFile As String
    Dim RetVal As Long
    Dim myDbHandle As LongPtr
    Dim myStmtHandle As LongPtr
    
    InitReturn = SQLite3Initialize
    If InitReturn <> SQLITE_INIT_OK Then
        Debug.Print "Error Initializing SQLite. Error: " & Err.LastDllError
        Exit Sub
    End If
    
    testFile = "C:\temp\Test01.db3"
    RetVal = SQLite3Open(testFile, myDbHandle)
    Debug.Print "SQLite3Open returned " & RetVal

    RetVal = SQLite3PrepareV2(myDbHandle, "CREATE TABLE MySecondTable (TheId INTEGER, TheText TEXT, TheValue REAL)", myStmtHandle)
    Debug.Print "SQLite3PrepareV2 returned " & RetVal
    
    RetVal = SQLite3Step(myStmtHandle)
    Debug.Print "SQLite3Step returned " & RetVal

    RetVal = SQLite3Finalize(myStmtHandle)
    Debug.Print "SQLite3Finalize returned " & RetVal
    
    RetVal = SQLite3PrepareV2(myDbHandle, "INSERT INTO MySecondTable Values (123, 'ABC', 42.1)", myStmtHandle)
    Debug.Print "SQLite3PrepareV2 returned " & RetVal
    RetVal = SQLite3Step(myStmtHandle)
    Debug.Print "SQLite3Step returned " & RetVal

    RetVal = SQLite3PrepareV2(myDbHandle, "INSERT INTO MySecondTable Values (456, 'DEF', 12.3)", myStmtHandle)
    Debug.Print "SQLite3PrepareV2 returned " & RetVal
    RetVal = SQLite3Step(myStmtHandle)
    Debug.Print "SQLite3Step returned " & RetVal
    
    RetVal = SQLite3PrepareV2(myDbHandle, "INSERT INTO MySecondTable Values (789, 'GHI', 45.6)", myStmtHandle)
    Debug.Print "SQLite3PrepareV2 returned " & RetVal
    RetVal = SQLite3Step(myStmtHandle)
    Debug.Print "SQLite3Step returned " & RetVal
    
    RetVal = SQLite3Finalize(myStmtHandle)
    Debug.Print "SQLite3Finalize returned " & RetVal

    RetVal = SQLite3Close(myDbHandle)


End Sub

Sub ボタン2_Click()
    Dim testFile As String
    Dim RetVal As Long
    Dim myDbHandle As LongPtr
    Dim myStmtHandle As LongPtr
    
    testFile = "C:\temp\Test01.db3"
    
    RetVal = SQLite3Open(testFile, myDbHandle)
    Debug.Print "SQLite3Open returned " & RetVal
    
    RetVal = SQLite3PrepareV2(myDbHandle, "SELECT * FROM MySecondTable", myStmtHandle)
    Debug.Print "SQLite3PrepareV2 returned " & RetVal
    
    RetVal = SQLite3Step(myStmtHandle)
    Debug.Print "SQLite3Step returned " & RetVal

    r = 1
    Do While RetVal <> SQLITE_DONE
        Cells(r, 1).Value = SQLite3ColumnText(myStmtHandle, 0)
        Cells(r, 2).Value = SQLite3ColumnText(myStmtHandle, 1)
        Cells(r, 3).Value = SQLite3ColumnText(myStmtHandle, 2)
        RetVal = SQLite3Step(myStmtHandle)
        r = r + 1
    Loop
    
    RetVal = SQLite3Finalize(myStmtHandle)
    Debug.Print "SQLite3Finalize returned " & RetVal
    
    RetVal = SQLite3Close(myDbHandle)

End Sub

実行結果。
sqliteExcel

DB Browser for SQLiteでDBファイルの内容を確認。
sqliteExcel02

データベースのデフォルトデータを作成する用途に便利だと思いました。