Skip to content

Commit ca6ba0e

Browse files
govert_cpgovert_cp
authored andcommitted
* Added support for sqlite3_open_v2 to allow opening of database as read-only.
1 parent ea19ce4 commit ca6ba0e

10 files changed

Lines changed: 298 additions & 832 deletions

File tree

BuildProcessTemplates/DefaultTemplate.11.1.xaml

Lines changed: 0 additions & 543 deletions
This file was deleted.

BuildProcessTemplates/LabDefaultTemplate.11.xaml

Lines changed: 0 additions & 208 deletions
This file was deleted.

BuildProcessTemplates/UpgradeTemplate.xaml

Lines changed: 0 additions & 76 deletions
This file was deleted.

Distribution/ChangeLog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Version 0.9 (8 November 2012)
2+
=============================
3+
* Add support for sqlite_open_v2
4+
15
Version 0.8 (24 July 2012)
26
==========================
37
* Added (non-incremental) Blob access, and small test.

Distribution/SQLiteForExcel.xls

-42 KB
Binary file not shown.
18.5 KB
Binary file not shown.

Source/SQLite3VBAModules/Sqlite3.bas

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ Public Const SQLITE_IOERR_CLOSE As Long = 4106 '(SQLITE_IOERR | (16
6868
Public Const SQLITE_IOERR_DIR_CLOSE As Long = 4362 '(SQLITE_IOERR | (17<<8))
6969
Public Const SQLITE_LOCKED_SHAREDCACHE As Long = 265 '(SQLITE_LOCKED | (1<<8) )
7070

71+
' Flags For File Open Operations
72+
Public Const SQLITE_OPEN_READONLY As Long = 1 ' Ok for sqlite3_open_v2()
73+
Public Const SQLITE_OPEN_READWRITE As Long = 2 ' Ok for sqlite3_open_v2()
74+
Public Const SQLITE_OPEN_CREATE As Long = 4 ' Ok for sqlite3_open_v2()
75+
Public Const SQLITE_OPEN_DELETEONCLOSE As Long = 8 ' VFS only
76+
Public Const SQLITE_OPEN_EXCLUSIVE As Long = 16 ' VFS only
77+
Public Const SQLITE_OPEN_AUTOPROXY As Long = 32 ' VFS only
78+
Public Const SQLITE_OPEN_URI As Long = 64 ' Ok for sqlite3_open_v2()
79+
Public Const SQLITE_OPEN_MEMORY As Long = 128 ' Ok for sqlite3_open_v2()
80+
Public Const SQLITE_OPEN_MAIN_DB As Long = 256 ' VFS only
81+
Public Const SQLITE_OPEN_TEMP_DB As Long = 512 ' VFS only
82+
Public Const SQLITE_OPEN_TRANSIENT_DB As Long = 1024 ' VFS only
83+
Public Const SQLITE_OPEN_MAIN_JOURNAL As Long = 2048 ' VFS only
84+
Public Const SQLITE_OPEN_TEMP_JOURNAL As Long = 4096 ' VFS only
85+
Public Const SQLITE_OPEN_SUBJOURNAL As Long = 8192 ' VFS only
86+
Public Const SQLITE_OPEN_MASTER_JOURNAL As Long = 16384 ' VFS only
87+
Public Const SQLITE_OPEN_NOMUTEX As Long = 32768 ' Ok for sqlite3_open_v2()
88+
Public Const SQLITE_OPEN_FULLMUTEX As Long = 65536 ' Ok for sqlite3_open_v2()
89+
Public Const SQLITE_OPEN_SHAREDCACHE As Long = 131072 ' Ok for sqlite3_open_v2()
90+
Public Const SQLITE_OPEN_PRIVATECACHE As Long = 262144 ' Ok for sqlite3_open_v2()
91+
Public Const SQLITE_OPEN_WAL As Long = 524288 ' VFS only
92+
7193
' Options for Text and Blob binding
7294
Private Const SQLITE_STATIC As Long = 0
7395
Private Const SQLITE_TRANSIENT As Long = -1
@@ -92,6 +114,7 @@ Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) A
92114
Private Declare Function sqlite3_stdcall_libversion Lib "SQLite3_StdCall" Alias "_sqlite3_stdcall_libversion@0" () As Long ' PtrUtf8String
93115
' Database connections
94116
Private Declare Function sqlite3_stdcall_open16 Lib "SQLite3_StdCall" Alias "_sqlite3_stdcall_open16@8" (ByVal pwsFileName As Long, ByRef hDb As Long) As Long ' PtrDb
117+
Private Declare Function sqlite3_stdcall_open_v2 Lib "SQLite3_StdCall" Alias "_sqlite3_stdcall_open_v2@16" (ByVal pwsFileName As Long, ByRef hDb As Long, ByVal iFlags As Long, ByVal zVfs As Long) As Long ' PtrDb
95118
Private Declare Function sqlite3_stdcall_close Lib "SQLite3_StdCall" Alias "_sqlite3_stdcall_close@4" (ByVal hDb As Long) As Long
96119
' Database connection error info
97120
Private Declare Function sqlite3_stdcall_errmsg Lib "SQLite3_StdCall" Alias "_sqlite3_stdcall_errmsg@4" (ByVal hDb As Long) As Long ' PtrUtf8String
@@ -198,8 +221,20 @@ End Function
198221
'=====================================================================================
199222
' Database connections
200223

201-
Public Function SQLite3Open(ByVal FileName As String, ByRef dbHandle As Long) As Long
202-
SQLite3Open = sqlite3_stdcall_open16(StrPtr(FileName), dbHandle)
224+
Public Function SQLite3Open(ByVal fileName As String, ByRef dbHandle As Long) As Long
225+
SQLite3Open = sqlite3_stdcall_open16(StrPtr(fileName), dbHandle)
226+
End Function
227+
228+
Public Function SQLite3OpenV2(ByVal fileName As String, ByRef dbHandle As Long, ByVal flags As Long, ByVal vfsName As String) As Long
229+
Dim bufFileName() As Byte
230+
Dim bufVfsName() As Byte
231+
bufFileName = StringToUtf8Bytes(fileName)
232+
If vfsName = Empty Then
233+
SQLite3OpenV2 = sqlite3_stdcall_open_v2(VarPtr(bufFileName(0)), dbHandle, flags, 0)
234+
Else
235+
bufVfsName = StringToUtf8Bytes(vfsName)
236+
SQLite3OpenV2 = sqlite3_stdcall_open_v2(VarPtr(bufFileName(0)), dbHandle, flags, VarPtr(bufVfsName(0)))
237+
End If
203238
End Function
204239

205240
Public Function SQLite3Close(ByVal dbHandle As Long) As Long

Source/SQLite3VBAModules/Sqlite3Demo.bas

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Public Sub AllTests()
1212

1313
TestVersion
1414
TestOpenClose
15+
TestOpenCloseV2
1516
TestError
1617
TestInsert
1718
TestSelect
@@ -20,6 +21,8 @@ Public Sub AllTests()
2021
TestStrings
2122
TestBackup
2223
TestBlob
24+
TestWriteReadOnly
25+
2326
SQLite3Free ' Quite optional
2427
End Sub
2528

@@ -60,6 +63,32 @@ Public Sub TestOpenClose()
6063

6164
End Sub
6265

66+
Public Sub TestOpenCloseV2()
67+
Dim testFile As String
68+
Dim myDbHandle As Long
69+
Dim myDbHandleV2 As Long
70+
Dim RetVal As Long
71+
72+
' Open the database in Read Write Access
73+
testFile = "C:\TestSqlite3ForExcel.db3"
74+
RetVal = SQLite3Open(testFile, myDbHandle)
75+
Debug.Print "SQLite3Open returned " & RetVal
76+
77+
' Open the database in Read Only Access
78+
testFile = "C:\TestSqlite3ForExcel.db3"
79+
RetVal = SQLite3OpenV2(testFile, myDbHandleV2, SQLITE_OPEN_READONLY, "")
80+
Debug.Print "SQLite3OpenV2 returned " & RetVal
81+
82+
RetVal = SQLite3Close(myDbHandleV2)
83+
Debug.Print "SQLite3Close V2 returned " & RetVal
84+
85+
RetVal = SQLite3Close(myDbHandle)
86+
Debug.Print "SQLite3Close returned " & RetVal
87+
88+
Kill testFile
89+
90+
End Sub
91+
6392
Public Sub TestError()
6493
Dim myDbHandle As Long
6594
Dim RetVal As Long
@@ -1113,6 +1142,78 @@ Public Sub TestBlob()
11131142
End Sub
11141143

11151144

1145+
Public Sub TestWriteReadOnly()
1146+
Dim testFile As String
1147+
Dim myDbHandle As Long
1148+
Dim myDbHandleV2 As Long
1149+
Dim myStmtHandle As Long
1150+
Dim RetVal As Long
1151+
1152+
' Open the database in Read Write Access
1153+
testFile = "C:\TestSqlite3ForExcel.db3"
1154+
RetVal = SQLite3Open(testFile, myDbHandle)
1155+
Debug.Print "SQLite3Open returned " & RetVal
1156+
1157+
' Open the database in Read Only Access
1158+
testFile = "C:\TestSqlite3ForExcel.db3"
1159+
RetVal = SQLite3OpenV2(testFile, myDbHandleV2, SQLITE_OPEN_READONLY, Empty)
1160+
Debug.Print "SQLite3OpenV2 returned " & RetVal
1161+
1162+
' Create the sql statement - getting a StmtHandle back
1163+
RetVal = SQLite3PrepareV2(myDbHandle, "CREATE TABLE MyFirstTable (TheId INTEGER, TheText TEXT, TheValue REAL)", myStmtHandle)
1164+
Debug.Print "SQLite3PrepareV2 returned " & RetVal
1165+
1166+
' Start running the statement
1167+
RetVal = SQLite3Step(myStmtHandle)
1168+
Debug.Print "SQLite3Step returned " & RetVal
1169+
1170+
' Finalize (delete) the statement
1171+
RetVal = SQLite3Finalize(myStmtHandle)
1172+
Debug.Print "SQLite3Finalize returned " & RetVal
1173+
1174+
' Create the sql statement - getting a StmtHandle back with Read Only
1175+
RetVal = SQLite3PrepareV2(myDbHandleV2, "CREATE TABLE MySecondTable (TheId INTEGER, TheText TEXT, TheValue REAL)", myStmtHandle)
1176+
'RetVal = SQLite3PrepareV2(myDbHandleV2, "SELECT * FROM MyFirstTable", myStmtHandle)
1177+
Debug.Print "SQLite3PrepareV2 returned " & RetVal
1178+
1179+
' Start running the statement with Read Only
1180+
RetVal = SQLite3Step(myStmtHandle)
1181+
Debug.Print "SQLite3Step returned " & RetVal
1182+
1183+
If RetVal = SQLITE_READONLY Then
1184+
Debug.Print "Cannot Write in Read Only database"
1185+
End If
1186+
1187+
' Finalize (delete) the statement with Read Only
1188+
RetVal = SQLite3Finalize(myStmtHandle)
1189+
Debug.Print "SQLite3Finalize returned " & RetVal
1190+
1191+
' Create the sql statement - getting a StmtHandle back with Read Only
1192+
RetVal = SQLite3PrepareV2(myDbHandleV2, "SELECT * FROM MyFirstTable", myStmtHandle)
1193+
Debug.Print "SQLite3PrepareV2 returned " & RetVal
1194+
1195+
' Start running the statement with Read Only
1196+
RetVal = SQLite3Step(myStmtHandle)
1197+
Debug.Print "SQLite3Step returned " & RetVal
1198+
1199+
If RetVal = SQLITE_DONE Then
1200+
Debug.Print "But Reading is granted on Read Only database"
1201+
End If
1202+
1203+
' Finalize (delete) the statement with Read Only
1204+
RetVal = SQLite3Finalize(myStmtHandle)
1205+
Debug.Print "SQLite3Finalize returned " & RetVal
1206+
1207+
RetVal = SQLite3Close(myDbHandleV2)
1208+
Debug.Print "SQLite3Close V2 returned " & RetVal
1209+
1210+
RetVal = SQLite3Close(myDbHandle)
1211+
Debug.Print "SQLite3Close returned " & RetVal
1212+
1213+
Kill testFile
1214+
1215+
End Sub
1216+
11161217

11171218
' SQLite3 Helper Functions
11181219
Public Function SQLite3ExecuteNonQuery(ByVal dbHandle As Long, ByVal SqlCommand As String) As Long
@@ -1161,3 +1262,4 @@ Public Sub SQLite3ExecuteQuery(ByVal dbHandle As Long, ByVal sqlQuery As String)
11611262
RetVal = SQLite3Finalize(stmtHandle)
11621263
Debug.Print "SQLite3Finalize returned " & RetVal
11631264
End Sub
1265+

0 commit comments

Comments
 (0)