|
' lance une requete sur la base et affiche les résultats dans le tableau |
|
Public Sub SQLite_LinkTableDB(ByVal path_db As String, ByVal myquery As String, ByVal ws As Worksheet, ByVal tab_name As String) |
|
Dim myDbHandle As Long |
|
Dim myStmtHandle As Long |
|
Dim RetVal As Long |
|
Dim compteur_lignes As Long |
|
|
|
perf_arr = optim_perf_deb |
|
|
|
' on nettoie le tableau de destination |
|
Set myTab = ws.Range(tab_name) |
|
clearTab ws, tab_name |
|
|
|
' on initie la connexion à la db |
|
RetVal = SQLite3Open(path_db, myDbHandle) |
|
|
|
' on prepare la requete SQL |
|
RetVal = SQLite3PrepareV2(myDbHandle, myquery, myStmtHandle) |
|
|
|
' on reçoit les resultats de la requete ligne par ligne |
|
' (à chaque fois qu'on appelle SQLite3Step, ça renvoie une ligne de résultat) |
|
|
|
' on reçoit la première ligne de résultat |
|
RetVal = SQLite3Step(myStmtHandle) |
|
' s'il y a une erreur on arrete tout |
|
If RetVal = 21 Then |
|
Debug.Print "Erreur in SQLite_LinkTableDB request : " & myquery |
|
Exit Sub |
|
End If |
|
|
|
' CORE : on reçoit les autres lignes de résultat |
|
compteur_lignes = 1 |
|
While RetVal = SQLITE_ROW And compteur_lignes < 1000000 ' on limite le nombre de lignes à 1 Million |
|
colCount = SQLite3ColumnCount(myStmtHandle) |
|
For i = 0 To min_long(colCount - 1, myTab.Columns.Count - 1) |
|
colName = SQLite3ColumnName(myStmtHandle, i) |
|
colType = SQLite3ColumnType(myStmtHandle, i) |
|
colTypeName = TypeName(colType) |
|
colValue = ColumnValue(myStmtHandle, i, colType) |
|
myTab.Columns(i + 1).Rows(compteur_lignes).Value = colValue |
|
Next |
|
RetVal = SQLite3Step(myStmtHandle) |
|
compteur_lignes = compteur_lignes + 1 |
|
Wend |
|
|
|
' on ferme la connexion à la base de données |
|
RetVal = SQLite3Finalize(myStmtHandle) |
|
RetVal = SQLite3Close(myDbHandle) |
|
|
|
' on termine l'optimisation de perfs |
|
optim_perf_fin perf_arr |
|
End Sub |
|
|
|
' execute une requete et renvoie le statut (ne pas utiliser pour des select qui renvoient plusieurs lignes) |
|
Public Function executeQuery(ByVal path_db As String, ByVal myquery As String) As String |
|
Dim myDbHandle As Long |
|
Dim myStmtHandle As Long |
|
Dim RetVal As Long |
|
|
|
' on initie la connexion à la db |
|
RetVal = SQLite3Open(path_db, myDbHandle) |
|
If RetVal <> 0 Then |
|
|
|
' on prepare la requete SQL |
|
RetVal = SQLite3PrepareV2(myDbHandle, myquery, myStmtHandle) |
|
|
|
' on traite la réponse |
|
RetVal = SQLite3Step(myStmtHandle) |
|
If RetVal = SQLITE_ROW Then |
|
executeQuery = "SQLITE_ROW : " |
|
colCount = SQLite3ColumnCount(myStmtHandle) |
|
For i = 0 To min_long(colCount - 1, myTab.Columns.Count - 1) |
|
colName = SQLite3ColumnName(myStmtHandle, i) |
|
colType = SQLite3ColumnType(myStmtHandle, i) |
|
colTypeName = TypeName(colType) |
|
colValue = ColumnValue(myStmtHandle, i, colType) |
|
executeQuery = executeQuery & ";" & colValue |
|
Next |
|
Else |
|
executeQuery = str(RetVal) |
|
End If |
|
|
|
' on ferme la connexion à la base de données |
|
RetVal = SQLite3Finalize(myStmtHandle) |
|
RetVal = SQLite3Close(myDbHandle) |
|
End Function |
|
|
|
|
|
' ========================================================================== |
|
' ========================================================================== |
|
|
|
' utile pour tester les appels à l'API SQLite |
|
Public Sub TestVersionSQLite() |
|
Debug.Print SQLite3LibVersion() |
|
End Sub |
|
|
|
' renvoie la valeur de la colonne à partir de son index, de son type et du handle sur la réponse de la base (stmtHandle) |
|
Function ColumnValue(ByVal stmtHandle As Long, ByVal ZeroBasedColIndex As Long, ByVal SQLiteType As Long) As Variant |
|
Select Case SQLiteType |
|
Case SQLITE_INTEGER: |
|
ColumnValue = SQLite3ColumnInt32(stmtHandle, ZeroBasedColIndex) |
|
Case SQLITE_FLOAT: |
|
ColumnValue = SQLite3ColumnDouble(stmtHandle, ZeroBasedColIndex) |
|
Case SQLITE_TEXT: |
|
ColumnValue = SQLite3ColumnText(stmtHandle, ZeroBasedColIndex) |
|
Case SQLITE_BLOB: |
|
ColumnValue = SQLite3ColumnText(stmtHandle, ZeroBasedColIndex) |
|
Case SQLITE_NULL: |
|
ColumnValue = Null |
|
End Select |
|
End Function |
|
|
|
|
|
' ========================================================================== |
|
' TESTS UNITAIRES |
|
' ========================================================================== |
|
|
|
Sub testunit_SQLite_LinkTableDB() |
|
initialisation |
|
myquery = "SELECT * FROM TAB_OGD WHERE commanditaire = ""DPI-DIN""" |
|
SQLite_LinkTableDB gPATH_DB, myquery, Sheets("Ref_OGD"), "TAB_OGD" |
|
End Sub |