我有一个可能很难解决的问题,但我在文档中找不到任何内容,也没有在StackOverflow等任何网站中找到。
我正在将信息从CSV导入到SQLite3数据库,为此我使用以下例程来支持我的导入例程
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
我的问题是:
如何创建数据库文件?
我在cmd中使用从SQLite3命令创建的文件并从中导入csv,但这不是我的Excel需求的好方法。
提前致谢!
答案 0 :(得分:1)
我刚刚找到了这样做的方法。
最简单的方法是使用以下代码:
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
然后,要创建数据库文件,我们只需要使用:
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
答案 1 :(得分:0)
'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