Visual Basic 6

Nous pouvons accéder à la base de données à l'aide d'un pilote ODBC SQLite.

Suivez simplement les instructions sur la page de ODBC.


Usage

Ci-dessous est un exemple utilisant ADO.

Vous pouvez utiliser un code similaire pour un nœud secondaire en changeant simplement la chaîne URI.


Code d'exemple
Option Explicit

Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

Public Sub Main()
  On Error GoTo locError

  Dim Conn As ADODB.Connection
  Dim Rst As ADODB.Recordset
  Dim URI As String
  Dim Status As String


  ' Open a connection
  URI = "file:C:\mydb.db?node=primary&bind=tcp://0.0.0.0:1234"
  Set Conn = New ADODB.Connection
  Conn.Open "DRIVER=SQLite3 ODBC Driver;Database=" & URI & ";Timeout=10000;"


  ' Check if the database is ready
  Do
    Set Rst = New ADODB.Recordset
    Rst.Open "PRAGMA sync_status", Conn, , , adCmdText
    If Rst.EOF Then
      MsgBox "Could not run 'PRAGMA sync_status'", vbExclamation
      GoTo locExit
    End If
    Status = Rst!sync_status
    Debug.Print Status
    If InStr(Status, """db_is_ready"": true") > 0 Then Exit Do
    Sleep 200
  Loop


  ' Create a table and populate it
  Conn.Execute "CREATE TABLE IF NOT EXISTS t1 (name)"
  Conn.Execute "INSERT INTO t1 VALUES ('1st')"
  Conn.Execute "INSERT INTO t1 VALUES ('2nd')"
  Conn.Execute "INSERT INTO t1 VALUES ('3rd')"


  ' Fetch the table data
  Set Rst = New ADODB.Recordset
  Rst.Open "t1", Conn, , , adCmdTable
  Do While Not Rst.EOF
    Debug.Print "  " & Rst!Name
    Rst.MoveNext
  Loop


locExit:

  ' Clean up

  If Not Rst Is Nothing Then
    If Rst.State = adStateOpen Then Rst.Close
  End If
  Set Rst = Nothing

  If Not Conn Is Nothing Then
    If Conn.State = adStateOpen Then Conn.Close
  End If
  Set Conn = Nothing

  Exit Sub

locError:

  If Err Then
    MsgBox Err.Source & ": " & Err.Description, , "Error"
  End If

  GoTo locExit

End Sub