3

I have a question that may be fairly simple to solve, but I can't find anything in the documentation nor in any website such as StackOverflow.

I'm importing information from a CSV to a SQLite3 database and to do so I use the following routine as to support my importation routine

  Sub runSQLiteQuery(path As String, strSQL As String)    

    Dim conn As Object, rst As Object

    Set conn = CreateObject("ADODB.Connection")
    Set rst = CreateObject("ADODB.Recordset")

    ' OPEN CONNECTION
    conn.Open "DRIVER=SQLite3 ODBC Driver;Database=" & path & ";"

    ' OPEN RECORDSET
    rst.Open strSQL, conn
    rst.Close
    ' FREE RESOURCES
    Set rst = Nothing: Set conn = Nothing

End Sub

My question is:

How do I create the database file in the first place?

I'm using a file created from SQLite3 command in cmd and importing the csv from that, but that's not a good solution for my Excel needs.

Thanks in advance!

2 Answers 2

4

I just found the way to do it.

The easiest way is to use the following code:

Sub runSQLiteQuery(path As String, strSQL As String)    

    Dim conn As Object, rst As Object

    Set conn = CreateObject("ADODB.Connection")
    Set rst = CreateObject("ADODB.Recordset")

    ' OPEN CONNECTION
    conn.Open "DRIVER=SQLite3 ODBC Driver;Database=" & path & ";"

    ' OPEN RECORDSET
    rst.Open strSQL, conn
    rst.Close
    ' FREE RESOURCES
    Set rst = Nothing: Set conn = Nothing

End Sub

Then, to create the database file we just need to use:

Sub startDb()
    Dim dbPath As String
    dbPath = Application.ActiveWorkbook.path

    ' Checks if Db exists. If not, creates
    If Dir(dbPath & "Db.s3db", vbDirectory) = "" Then Call runSQLiteQuery(dbPath & "\Db.s3db", "SELECT 1;")
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

Please have a look at Can I answer my own question? and come back two days later and check as answered if you have more than 15 reputation.
1
'use excel 2016 vba    
Dim conn As Object
Set conn = CreateObject("ADODB.Connection")
newdb = "F:\sqlite3win64\test.db3"
conn.Open "DRIVER=SQLite3 ODBC Driver;Database=" & newdb  

1 Comment

Welcome to StackOverflow! While this answer may solve the OP's issues, it's recommended to provide an explanation about the written code to make your answer more understandable for the community and also to improve its quality

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.