Open database in read_create with sqlite_open_v2

Feb 24, 2015 at 12:45 PM
Edited Feb 24, 2015 at 2:53 PM
Hello I am trying to use the TestOpenCloseV2 function using modified in this way
Public Sub TestOpenCloseV2()
    Dim testFile As String
    #If Win64 Then
    Dim myDbHandle As LongPtr
    Dim myDbHandleV2 As LongPtr
    #Else
    Dim myDbHandle As Long
    Dim myDbHandleV2 As Long
    #End If
    Dim RetVal As Long
    
    ' Open the database in Read Write Access
    testFile = "D:\TestSqlite3ForExcel.db3"
    RetVal = SQLite3Open(testFile, myDbHandle)
    Debug.Print "SQLite3Open returned " & RetVal
    
    ' Open the database in Read Create Access
    testFile = "D:\TestSqlite3ForExcel.db3"
    RetVal = SQLite3OpenV2(testFile, myDbHandleV2, SQLITE_OPEN_CREATE, "")
    Debug.Print "SQLite3OpenV2 returned " & RetVal
    
    RetVal = SQLite3Close(myDbHandleV2)
    Debug.Print "SQLite3Close V2 returned " & RetVal
    
    RetVal = SQLite3Close(myDbHandle)
    Debug.Print "SQLite3Close returned " & RetVal
     On Error Resume Next
    
    Kill testFile

End Sub
but it keeps on returning
SQLite3OpenV2 returned 21
Am I doing something wrong?

Regards and Thanks in advance
Feb 24, 2015 at 2:22 PM
Don't you need to run sqlite3_close first before reopening in another mode?

RBS
Feb 24, 2015 at 2:51 PM
Edited Feb 24, 2015 at 2:53 PM
I have commented the previous lines in this way:
Public Sub TestOpenCloseV2()
    Dim testFile As String
    #If Win64 Then
    Dim myDbHandle As LongPtr
    Dim myDbHandleV2 As LongPtr
    #Else
    Dim myDbHandle As Long
    Dim myDbHandleV2 As Long
    #End If
    Dim RetVal As Long
    
    ' Open the database in Read Write Access
    'testFile = "D:\TestSqlite3ForExcel.db3"
   ' RetVal = SQLite3Open(testFile, myDbHandle)
   ' Debug.Print "SQLite3Open returned " & RetVal
    
    ' Open the database in Read Create Access
    testFile = "D:\TestSqlite3ForExcel.db"
    RetVal = SQLite3OpenV2(testFile, myDbHandleV2, SQLITE_OPEN_CREATE, "")
    Debug.Print "SQLite3OpenV2 returned " & RetVal
but still I get 21 as return value
I have tried to open a db that already exist and it worked perfectly with the above code.

Any idea? maybe is a permission issue?
Feb 24, 2015 at 3:26 PM
Edited Feb 24, 2015 at 3:26 PM
OK I've fixed it!

to me this looks more like an SQLite3 issue than something else.
Anyway as is stated in here: https://www.sqlite.org/c3ref/open.html the sqlite3_open_v2 is taking the following flags:
SQLITE_OPEN_READONLY
SQLITE_OPEN_READWRITE
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
as soon as I modified my code to :
    testFile = "D:\TestSqlite3ForExcel.db"
    RetVal = SQLite3OpenV2(testFile, myDbHandleV2, 6, "")
    Debug.Print "SQLite3OpenV2 returned " & RetVal
Everything worked charmly :)
Marked as answer by melisc on 2/24/2015 at 8:26 AM
Feb 24, 2015 at 3:27 PM
Try Empty as the last argument for SQLite3OpenV2.

RBS