Skip to content

Commit 44b6751

Browse files
govert_cpgovert_cp
authored andcommitted
* Added (non-incremental) Blob access, and small test.
* Add support for 64-bit Excel 2010. The SQLiteForExcel_64.xlsm has VBA code that supports 32-bit and 64-bit versions of Excel. * Updated Distribution version of SQLite3.dll to 3.7.13 and added x64\SQLite3.dll.
1 parent 968fd7b commit 44b6751

9 files changed

Lines changed: 2047 additions & 0 deletions

File tree

Distribution/ChangeLog.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Version 0.8 (24 July 2012)
2+
==========================
3+
* Added (non-incremental) Blob access, and small test.
4+
* Add support for 64-bit Excel 2010.
5+
The SQLiteForExcel_64.xlsm has VBA code that supports 32-bit and 64-bit versions of Excel.
6+
* Updated Distribution version of SQLite3.dll to 3.7.13 and added x64\SQLite3.dll.
7+
18
Version 0.7 (25 June 2011)
29
==========================
310
* Fixed Unicode string conversion bug.

Distribution/SQLiteForExcel.xls

14 KB
Binary file not shown.
69.3 KB
Binary file not shown.

Distribution/sqlite3.dll

36.5 KB
Binary file not shown.

Distribution/x64/sqlite3.dll

1.12 MB
Binary file not shown.

Source/SQLite3VBAModules/Sqlite3.bas

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ Private Const SQLITE_TRANSIENT As Long = -1
7676
Private Const CP_UTF8 As Long = 65001
7777
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
7878
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cbMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
79+
Private Declare Sub RtlMoveMemory Lib "kernel32" (ByVal pDest As Long, ByVal pSource As Long, ByVal length As Long)
7980
Private Declare Function lstrcpynW Lib "kernel32" (ByVal pwsDest As Long, ByVal pwsSource As Long, ByVal cchCount As Long) As Long
8081
Private Declare Function lstrcpyW Lib "kernel32" (ByVal pwsDest As Long, ByVal pwsSource As Long) As Long
8182
Private Declare Function lstrlenW Lib "kernel32" (ByVal pwsString As Long) As Long
@@ -282,6 +283,18 @@ Public Function SQLite3ColumnDate(ByVal stmtHandle As Long, ByVal ZeroBasedColIn
282283
SQLite3ColumnDate = FromJulianDay(sqlite3_stdcall_column_double(stmtHandle, ZeroBasedColIndex))
283284
End Function
284285

286+
Public Function SQLite3ColumnBlob(ByVal stmtHandle As Long, ByVal ZeroBasedColIndex As Long) As Byte()
287+
Dim ptr As Long
288+
Dim length As Long
289+
Dim buf() As Byte
290+
291+
ptr = sqlite3_stdcall_column_blob(stmtHandle, ZeroBasedColIndex)
292+
length = sqlite3_stdcall_column_bytes(stmtHandle, ZeroBasedColIndex)
293+
ReDim buf(length - 1)
294+
RtlMoveMemory VarPtr(buf(0)), ptr, length
295+
SQLite3ColumnBlob = buf
296+
End Function
297+
285298
'=====================================================================================
286299
' Statement bindings
287300

@@ -301,6 +314,12 @@ Public Function SQLite3BindDate(ByVal stmtHandle As Long, ByVal OneBasedParamInd
301314
SQLite3BindDate = sqlite3_stdcall_bind_double(stmtHandle, OneBasedParamIndex, ToJulianDay(Value))
302315
End Function
303316

317+
Public Function SQLite3BindBlob(ByVal stmtHandle As Long, ByVal OneBasedParamIndex As Long, ByRef Value() As Byte) As Long
318+
Dim length As Long
319+
length = UBound(Value) - LBound(Value) + 1
320+
SQLite3BindBlob = sqlite3_stdcall_bind_blob(stmtHandle, OneBasedParamIndex, VarPtr(Value(0)), length, SQLITE_TRANSIENT)
321+
End Function
322+
304323
Public Function SQLite3BindNull(ByVal stmtHandle As Long, ByVal OneBasedParamIndex As Long) As Long
305324
SQLite3BindNull = sqlite3_stdcall_bind_null(stmtHandle, OneBasedParamIndex)
306325
End Function

Source/SQLite3VBAModules/Sqlite3Demo.bas

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Public Sub AllTests()
1919
TestDates
2020
TestStrings
2121
TestBackup
22+
TestBlob
2223
SQLite3Free ' Quite optional
2324
End Sub
2425

@@ -850,6 +851,7 @@ Public Sub TestStrings()
850851

851852
Dim myString1 As String
852853
Dim myString2 As String
854+
Dim myLongString As String
853855
Dim myStringResult As String
854856

855857
Debug.Print "----- TestStrings Start -----"
@@ -860,6 +862,7 @@ Public Sub TestStrings()
860862
Debug.Print "SQLite3Open returned " & RetVal
861863

862864
myString2 = ""
865+
myLongString = String(10000, "A")
863866

864867
'------------------------
865868
' Create the table
@@ -907,6 +910,23 @@ Public Sub TestStrings()
907910
Beep
908911
End If
909912

913+
RetVal = SQLite3Reset(myStmtHandle)
914+
If RetVal <> SQLITE_OK Then
915+
Debug.Print "SQLite3Reset returned " & RetVal, SQLite3ErrMsg(myDbHandle)
916+
Beep
917+
End If
918+
919+
RetVal = SQLite3BindText(myStmtHandle, 1, myLongString)
920+
If RetVal <> SQLITE_OK Then
921+
Debug.Print "SQLite3Bind returned " & RetVal, SQLite3ErrMsg(myDbHandle)
922+
Beep
923+
End If
924+
925+
RetVal = SQLite3Step(myStmtHandle)
926+
If RetVal <> SQLITE_DONE Then
927+
Debug.Print "SQLite3Step returned " & RetVal, SQLite3ErrMsg(myDbHandle)
928+
Beep
929+
End If
910930
' Finalize the statement
911931
RetVal = SQLite3Finalize(myStmtHandle)
912932
Debug.Print "SQLite3Finalize returned " & RetVal
@@ -937,6 +957,16 @@ Public Sub TestStrings()
937957
Debug.Print "No row found"
938958
End If
939959

960+
RetVal = SQLite3Step(myStmtHandle)
961+
If RetVal = SQLITE_ROW Then
962+
' We have access to the result columns here.
963+
myStringResult = SQLite3ColumnText(myStmtHandle, 0)
964+
965+
Debug.Print "Long String is the same: " & (myStringResult = myLongString)
966+
ElseIf RetVal = SQLITE_DONE Then
967+
Debug.Print "No row found"
968+
End If
969+
940970
' Finalize (delete) the statement
941971
RetVal = SQLite3Finalize(myStmtHandle)
942972
Debug.Print "SQLite3Finalize returned " & RetVal
@@ -997,6 +1027,93 @@ Public Sub TestBackup()
9971027
Debug.Print "----- TestBackup End -----"
9981028
End Sub
9991029

1030+
Public Sub TestBlob()
1031+
Dim testFile As String
1032+
1033+
Dim myDbHandle As Long
1034+
Dim myStmtHandle As Long
1035+
Dim RetVal As Long
1036+
Dim stepMsg As String
1037+
Dim i As Long
1038+
1039+
Dim myBlob(2) As Byte
1040+
Dim myBlobResult() As Byte
1041+
1042+
Debug.Print "----- TestBlob Start -----"
1043+
1044+
' Open the database - getting a DbHandle back
1045+
testFile = "C:\TestSqlite3ForExcel.db3"
1046+
RetVal = SQLite3Open(testFile, myDbHandle)
1047+
Debug.Print "SQLite3Open returned " & RetVal
1048+
1049+
myBlob(0) = 90
1050+
myBlob(1) = 91
1051+
myBlob(2) = 92
1052+
1053+
'------------------------
1054+
' Create the table
1055+
' ================
1056+
' (I've got no error checking here...)
1057+
SQLite3ExecuteNonQuery myDbHandle, "CREATE TABLE MyBlobTable (MyValue BLOB)"
1058+
1059+
'-------------------------
1060+
' Prepare an insert statement with parameters
1061+
' ===============
1062+
' Create the sql statement - getting a StmtHandle back
1063+
RetVal = SQLite3PrepareV2(myDbHandle, "INSERT INTO MyBlobTable Values (@SomeString)", myStmtHandle)
1064+
If RetVal <> SQLITE_OK Then
1065+
Debug.Print "SQLite3PrepareV2 returned " & SQLite3ErrMsg(myDbHandle)
1066+
Beep
1067+
End If
1068+
1069+
RetVal = SQLite3BindBlob(myStmtHandle, 1, myBlob)
1070+
If RetVal <> SQLITE_OK Then
1071+
Debug.Print "SQLite3Bind returned " & RetVal, SQLite3ErrMsg(myDbHandle)
1072+
Beep
1073+
End If
1074+
1075+
RetVal = SQLite3Step(myStmtHandle)
1076+
If RetVal <> SQLITE_DONE Then
1077+
Debug.Print "SQLite3Step returned " & RetVal, SQLite3ErrMsg(myDbHandle)
1078+
Beep
1079+
End If
1080+
1081+
' Finalize the statement
1082+
RetVal = SQLite3Finalize(myStmtHandle)
1083+
Debug.Print "SQLite3Finalize returned " & RetVal
1084+
1085+
'-------------------------
1086+
' Select statement
1087+
' ===============
1088+
' Create the sql statement - getting a StmtHandle back
1089+
' Now using named parameters!
1090+
RetVal = SQLite3PrepareV2(myDbHandle, "SELECT * FROM MyBlobTable", myStmtHandle)
1091+
Debug.Print "SQLite3PrepareV2 returned " & RetVal
1092+
1093+
RetVal = SQLite3Step(myStmtHandle)
1094+
If RetVal = SQLITE_ROW Then
1095+
' We have access to the result columns here.
1096+
myBlobResult = SQLite3ColumnBlob(myStmtHandle, 0)
1097+
For i = LBound(myBlobResult) To UBound(myBlobResult)
1098+
Debug.Print "Blob byte " & i & ": " & myBlobResult(i)
1099+
Next
1100+
ElseIf RetVal = SQLITE_DONE Then
1101+
Debug.Print "No row found"
1102+
End If
1103+
1104+
' Finalize (delete) the statement
1105+
RetVal = SQLite3Finalize(myStmtHandle)
1106+
Debug.Print "SQLite3Finalize returned " & RetVal
1107+
1108+
' Close the database
1109+
RetVal = SQLite3Close(myDbHandle)
1110+
Kill testFile
1111+
1112+
Debug.Print "----- TestBlob End -----"
1113+
End Sub
1114+
1115+
1116+
10001117
' SQLite3 Helper Functions
10011118
Public Function SQLite3ExecuteNonQuery(ByVal dbHandle As Long, ByVal SqlCommand As String) As Long
10021119
Dim stmtHandle As Long

0 commit comments

Comments
 (0)